From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4D4BB6E1C9; Mon, 27 Dec 2021 16:44:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4D4BB6E1C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1640612672; bh=6xzEcJ/vf/iejZ8e7AKnDztqxoRe9J48AmUKL5ldFPE=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=m8CLG89D1mL78pGfV85TQaT8GzGkpCX2Ke2q+vxXdB3LE1xFPtIITN9V1H6wJLki7 EaTIFhXhG/vr4WibBWP1RJt9PpKV2fzTvIOB5XQ8h/9IMvRswtD8n42QSGgyZTRzhj yau5bl7QWIH4/BccBCnhqkFP/Bh7Y8laKnMul7yk= Received: from smtp3.mail.ru (smtp3.mail.ru [94.100.179.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id DF4CE6E1C9 for ; Mon, 27 Dec 2021 16:44:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DF4CE6E1C9 Received: by smtp3.mail.ru with esmtpa (envelope-from ) id 1n1qIf-00040N-Sp; Mon, 27 Dec 2021 16:44:30 +0300 To: Sergey Ostanevich , Igor Munkin Date: Mon, 27 Dec 2021 16:42:37 +0300 Message-Id: <20211227134237.2942-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD94D5EF110843E6A6756D129FFADCD37D44B66CFF8B7714B73182A05F538085040A08277220505104F79B1F1B17A6E5C5009FABC965A7D350F7166C0EA46F0FF2A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BB17EE3498E810FEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063771C846A5973DEE7E8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80BCC745336A5DC40B2641DA304D0216E117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF80095D1E57F4578A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751F2CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6753C3A5E0A5AB5B7089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C5B1EC91705488372838C50FA628971E98B4B78650AA322159C2B6934AE262D3EE7EAB7254005DCED6BB2BF071322A0961E0A4E2319210D9B64D260DF9561598F01A9E91200F654B0588BE6097D4A97A98E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3435BBF0AC4E3A921C2FB9959A5D52E7F1AEC3DC30F6967E0485C938D3BD287F2EB26C0506DC2B27A41D7E09C32AA3244CEA154E2CE4E4ADF444E8D334873DA4E08894E9C85370243E927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojAMkO8M8XKUvyO5RMBy/RYg== X-Mailru-Sender: 3B9A0136629DC91206CBC582EFEF4CB412321D91E243A3622F5C14E94E8AE557A4F514C37033153EF2400F607609286E924004A7DEC283833C7120B22964430C52B393F8C72A41A84198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Give expected results for negative non-base-10 numbers in tonumber(). X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall This was undefined in Lua 5.1, but it's defined in 5.2. (cherry picked from f3cf0d6e15240098147437fed7bd436ff55fdf8c) `strtoul()` considers negative values as a valid input and silently converts them to the equivalent unsigned long value. As a result yielded value is unexpected to the user. This patch adds reading of a sign (if exists) from argument and provide the remaining part of the string as is if it starts with a digit or alphabetical symbol to be consistent with Lua 5.2. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#6548 --- Issue: https://github.com/tarantool/tarantool/issues/6548 Branch: https://github.com/tarantool/luajit/tree/skaplun/gh-noticket-tonumber-expected-results-full-ci Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-tonumber-expected-results-full-ci CI is red due to integration tests failes (same as on master) or due to connection errors. Side note: I suppose that undefinence Mike talking about is the following lines in the Lua 5.1 Reference manual [1]: | In base 10 (the default), the number can have a decimal part, as well | as an optional exponent part (see paragraph 2.1). In other bases, only | unsigned integers are accepted. In the Lua 5.2 Reference manual [2] they are deleted. src/lib_base.c | 27 +++++++++++------ ...onumber-negative-non-decimal-base.test.lua | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 test/tarantool-tests/tonumber-negative-non-decimal-base.test.lua diff --git a/src/lib_base.c b/src/lib_base.c index 3a757870..d61e8762 100644 --- a/src/lib_base.c +++ b/src/lib_base.c @@ -287,18 +287,27 @@ LJLIB_ASM(tonumber) LJLIB_REC(.) } else { const char *p = strdata(lj_lib_checkstr(L, 1)); char *ep; + unsigned int neg = 0; unsigned long ul; if (base < 2 || base > 36) lj_err_arg(L, 2, LJ_ERR_BASERNG); - ul = strtoul(p, &ep, base); - if (p != ep) { - while (lj_char_isspace((unsigned char)(*ep))) ep++; - if (*ep == '\0') { - if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u)) - setintV(L->base-1-LJ_FR2, (int32_t)ul); - else - setnumV(L->base-1-LJ_FR2, (lua_Number)ul); - return FFH_RES(1); + while (lj_char_isspace((unsigned char)(*p))) p++; + if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; } + if (lj_char_isalnum((unsigned char)(*p))) { + ul = strtoul(p, &ep, base); + if (p != ep) { + while (lj_char_isspace((unsigned char)(*ep))) ep++; + if (*ep == '\0') { + if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) { + if (neg) ul = -ul; + setintV(L->base-1-LJ_FR2, (int32_t)ul); + } else { + lua_Number n = (lua_Number)ul; + if (neg) n = -n; + setnumV(L->base-1-LJ_FR2, n); + } + return FFH_RES(1); + } } } } diff --git a/test/tarantool-tests/tonumber-negative-non-decimal-base.test.lua b/test/tarantool-tests/tonumber-negative-non-decimal-base.test.lua new file mode 100644 index 00000000..94df3b1f --- /dev/null +++ b/test/tarantool-tests/tonumber-negative-non-decimal-base.test.lua @@ -0,0 +1,29 @@ +local tap = require('tap') + +local test = tap.test('tonumber-negative-non-decimal-base') +test:plan(18) + +-- Test valid tonumber() with +- signs and non-10 base. +test:ok(tonumber('-010', 2) == -2, 'negative base 2') +test:ok(tonumber('-10', 8) == -8, 'negative base 8') +test:ok(tonumber('-0x10', 16) == -16, 'negative base 16') +test:ok(tonumber(' -1010 ', 2) == -10, 'negative base 2 with spaces') +test:ok(tonumber(' +1010 ', 2) == 10, 'positive base 2 with spaces') +test:ok(tonumber(' -012 ', 8) == -10, 'negative base 8 with spaces') +test:ok(tonumber(' +012 ', 8) == 10, 'positive base 8 with spaces') +test:ok(tonumber(' -10 ', 16) == -16, 'negative base 16 with spaces') +test:ok(tonumber(' +10 ', 16) == 16, 'positive base 16 with spaces') +test:ok(tonumber(' -1Z ', 36) == -36 - 35, 'negative base 36 with spaces') +test:ok(tonumber(' +1z ', 36) == 36 + 35, 'positive base 36 with spaces') +test:ok(tonumber('-fF', 16) == -(15 + (16 * 15)), 'negative base 16 mixed case') +test:ok(tonumber('-0ffffffFFFF', 16) - 1 == -2 ^ 40, 'negative base 16 long') + +-- Test invalid tonumber() for non-10 base. +test:ok(tonumber('-z1010 ', 2) == nil, 'incorrect notation in base 2') +test:ok(tonumber('--1010 ', 2) == nil, 'double minus sign') +test:ok(tonumber('-+1010 ', 2) == nil, 'minus plus sign') +test:ok(tonumber('- 1010 ', 2) == nil, 'space between sign and value') +test:ok(tonumber('-_1010 ', 2) == nil, + 'invalid character between sign and value') + +os.exit(test:check() and 0 or 1) -- 2.34.1 [1]: http://www.lua.org/manual/5.1/manual.html#pdf-tonumber [2]: http://www.lua.org/manual/5.2/manual.html#pdf-tonumber