Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: kostja@tarantool.org, tarantool-patches@freelists.org
Subject: Re: [tarantool-patches] [PATCH v2] box: fix long uri output in box.info()
Date: Tue, 21 Aug 2018 17:18:58 +0300	[thread overview]
Message-ID: <5DEBDD2B-2F9A-427F-A766-7A492A4C2F51@tarantool.org> (raw)
In-Reply-To: <20180821134542.ad2lsqpbnbsyujnk@esperanza>



> 21 авг. 2018 г., в 16:45, Vladimir Davydov <vdavydov.dev@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)

  reply	other threads:[~2018-08-21 14:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-21  6:27 Serge Petrenko
2018-08-21 13:45 ` Vladimir Davydov
2018-08-21 14:18   ` Serge Petrenko [this message]
2018-08-21 14:58     ` Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5DEBDD2B-2F9A-427F-A766-7A492A4C2F51@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [tarantool-patches] [PATCH v2] box: fix long uri output in box.info()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox