From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: [tarantool-patches] [PATCH v2] box: fix long uri output in box.info() From: Serge Petrenko In-Reply-To: <20180821134542.ad2lsqpbnbsyujnk@esperanza> Date: Tue, 21 Aug 2018 17:18:58 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <5DEBDD2B-2F9A-427F-A766-7A492A4C2F51@tarantool.org> References: <20180821062747.21357-1-sergepetrenko@tarantool.org> <20180821134542.ad2lsqpbnbsyujnk@esperanza> To: Vladimir Davydov Cc: kostja@tarantool.org, tarantool-patches@freelists.org List-ID: > 21 =D0=B0=D0=B2=D0=B3. 2018 =D0=B3., =D0=B2 16:45, Vladimir Davydov = =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0= ): >=20 > 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 =3D 0; >> + int maxlen =3D 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); >=20 > This is incorrect. >=20 > 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: >=20 > int buf_size =3D uri_format(NULL, 0, uri, false); > char *buf =3D malloc(buf_size); >=20 > 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); =20 - char name[FIBER_NAME_MAX]; + char name[APPLIER_SOURCE_MAXLEN]; int total =3D 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 =3D MIN(total, (int)sizeof(name) - 1); lua_pushstring(L, "peer"); lua_pushlstring(L, name, total); lua_settable(L, -3); --=20 2.15.2 (Apple Git-101.1)=