[tarantool-patches] [PATCH v2] box: fix long uri output in box.info()

Serge Petrenko sergepetrenko at tarantool.org
Tue Aug 21 17:18:58 MSK 2018



> 21 авг. 2018 г., в 16:45, Vladimir Davydov <vdavydov.dev at gmail.com> написал(а):
> 
> On Tue, Aug 21, 2018 at 09:27:47AM +0300, Serge Petrenko wrote:
>> diff --git a/src/uri.c b/src/uri.c
>> index 941e7bab9..77a1c88f6 100644
>> --- a/src/uri.c
>> +++ b/src/uri.c
>> @@ -6303,6 +6303,7 @@ int
>> uri_format(char *str, int len, const struct uri *uri, bool write_password)
>> {
>> 	int total = 0;
>> +	int maxlen = len - 1;
>> 	if (uri->scheme_len > 0) {
>> 		SNPRINT(total, snprintf, str, len, "%.*s://",
>> 			 (int)uri->scheme_len, uri->scheme);
>> @@ -6337,7 +6338,7 @@ uri_format(char *str, int len, const struct uri *uri, bool write_password)
>> 		SNPRINT(total, snprintf, str, len, "#%.*s",
>> 			(int)uri->fragment_len, uri->fragment);
>> 	}
>> -	return total;
>> +	return MIN(total, maxlen);
> 
> This is incorrect.
> 
> uri_format() should always return the would-be length of the uri string,
> even if there's not enough space in the buffer. This is consistent with
> snprintf() and this allows the caller to estimate the target buffer size
> by calling the function without a buffer:
> 
> 	int buf_size = uri_format(NULL, 0, uri, false);
> 	char *buf = malloc(buf_size);
> 
> That said, you should fix lbox_pushapplier() instead.

Fixed.

---
 src/box/lua/info.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/box/lua/info.c b/src/box/lua/info.c
index d6697df9c..d9ea73a64 100644
--- a/src/box/lua/info.c
+++ b/src/box/lua/info.c
@@ -95,9 +95,14 @@ lbox_pushapplier(lua_State *L, struct applier *applier)
 			       applier->last_row_time);
 		lua_settable(L, -3);
 
-		char name[FIBER_NAME_MAX];
+		char name[APPLIER_SOURCE_MAXLEN];
 		int total = uri_format(name, sizeof(name), &applier->uri, false);
-
+		/*
+		 * total can be greater than sizeof(name) if
+		 * name has insufficient length. Terminating
+		 * zero is ignored by lua_pushlstring.
+		 */
+		total = MIN(total, (int)sizeof(name) - 1);
 		lua_pushstring(L, "peer");
 		lua_pushlstring(L, name, total);
 		lua_settable(L, -3);
-- 
2.15.2 (Apple Git-101.1)


More information about the Tarantool-patches mailing list