From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id B58EC29A44 for ; Tue, 21 Aug 2018 02:27:54 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id apZ4bnNDN8rU for ; Tue, 21 Aug 2018 02:27:54 -0400 (EDT) Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 0766629A43 for ; Tue, 21 Aug 2018 02:27:53 -0400 (EDT) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v2] box: fix long uri output in box.info() Date: Tue, 21 Aug 2018 09:27:47 +0300 Message-Id: <20180821062747.21357-1-sergepetrenko@tarantool.org> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: kostja@tarantool.org Cc: tarantool-patches@freelists.org, Serge Petrenko lua_pushapplier() had an inexplicably small buffer for uri representation. Enlarged the buffer. Also uri_format() didn't ensure that its return value was not greater than the length of string passed to it. Also fixed and added a test case. Closes #3630 --- https://github.com/tarantool/tarantool/issues/3630 https://github.com/tarantool/tarantool/tree/sp/gh-3630-long-uri-fix Changes in v2: - fix uri_format() return value. - add a unit test for uri_format() src/box/lua/info.c | 2 +- src/uri.c | 3 ++- test/unit/uri.c | 29 ++++++++++++++++++++++++++++- test/unit/uri.result | 9 ++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/box/lua/info.c b/src/box/lua/info.c index d6697df9c..970c80c99 100644 --- a/src/box/lua/info.c +++ b/src/box/lua/info.c @@ -95,7 +95,7 @@ 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); lua_pushstring(L, "peer"); 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); } /* vim: set ft=ragel: */ diff --git a/test/unit/uri.c b/test/unit/uri.c index b69f92726..c3c20b176 100644 --- a/test/unit/uri.c +++ b/test/unit/uri.c @@ -59,10 +59,35 @@ test_invalid() return check_plan(); } +int +test_format() +{ + plan(5); + + struct uri uri; + + char str[] = "aurithatslongerthan16symbols:3303"; + char ret_str[16]; + char long_str[64]; + is(uri_parse(&uri, str), 0, "parse successful"); + is(uri_format(ret_str, sizeof(ret_str), &uri, false), + sizeof(ret_str) - 1, "uri_format returns correct length when"\ + " string is too long"); + is(strncmp(ret_str, str, sizeof(ret_str) - 1), 0, "string is correct"); + /* + * SNPRINT used in uri_format doesn't count the + * terminating zero. So expected length is sizeof(str) - 1. + */ + is(uri_format(long_str, sizeof(long_str), &uri, false), + sizeof(str) - 1, "uri_format returns correct length when string fits"); + is(strcmp(long_str, str), 0, "string is correct"); + return check_plan(); +} + int main(void) { - plan(63); + plan(64); /* General */ test("host", NULL, NULL, NULL, "host", NULL, NULL, NULL, NULL, 0); @@ -234,5 +259,7 @@ main(void) test_invalid(); + test_format(); + return check_plan(); } diff --git a/test/unit/uri.result b/test/unit/uri.result index 804560b69..7ca9ba2da 100644 --- a/test/unit/uri.result +++ b/test/unit/uri.result @@ -1,4 +1,4 @@ -1..63 +1..64 1..19 ok 1 - host: parse ok 2 - host: scheme @@ -1305,3 +1305,10 @@ ok 62 - subtests ok 1 - empty is invalid ok 2 - :// is invalid ok 63 - subtests + 1..5 + ok 1 - parse successful + ok 2 - uri_format returns correct length when string is too long + ok 3 - string is correct + ok 4 - uri_format returns correct length when string fits + ok 5 - string is correct +ok 64 - subtests -- 2.15.2 (Apple Git-101.1)