[tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64

  • From: Vladislav Shpilevoy <v.shpilevoy@xxxxxxxxxxxxx>
  • To: Kirill Shcherbatov <kshcherbatov@xxxxxxxxxxxxx>, tarantool-patches@xxxxxxxxxxxxx
  • Date: Tue, 17 Jul 2018 12:35:07 +0300

Thanks for the patch!

On 16/07/2018 19:52, Kirill Shcherbatov wrote:

Thank you for review.
===========================================

diff --git a/src/lua/init.c b/src/lua/init.c
index 9a96030..65addc1 100644
--- a/src/lua/init.c
+++ b/src/lua/init.c
@@ -222,7 +222,10 @@ lbox_tonumber64(struct lua_State *L)
                        if (argl == 0) {
                                lua_pushnil(L);
                        } else if (negative) {
-                               luaL_pushint64(L, -1 * (long long )result);
+                               if (result != 0 && result - 1 > INT64_MAX)

1. Please, add a comment about the things we have discussed as Alexander asked
in the previous message.

+                                       lua_pushnil(L);
+                               else
+                                       luaL_pushint64(L, -result);
                        } else {
                                luaL_pushuint64(L, result);
                        }
diff --git a/test/box/misc.result b/test/box/misc.result
index f332a8c..fa9926b 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -640,6 +640,25 @@ tostring(tonumber64('1234567890123456')) == 
'1234567890123456ULL'
  ---
  - true
  ...
+--
+-- gh-3466: Strange behaviour of tonumber64 function
+--
+tostring(tonumber64('18446744073709551615')) == '18446744073709551615ULL'
+---
+- true
+...
+tonumber64('18446744073709551616') == nil
+---
+- true
+...
+tostring(tonumber64('-9223372036854775808')) == '-9223372036854775808LL'
+---
+- true
+...
+tonumber64('-9223372036854775809') == nil
+---
+- true
+...
  tonumber64('0x12') == 18
  ---
  - true
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index e24228a..0b4ea21 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -165,6 +165,14 @@ tostring(tonumber64('12345678901234')) == '12345678901234'
  tostring(tonumber64('123456789012345')) == '123456789012345ULL'
  tostring(tonumber64('1234567890123456')) == '1234567890123456ULL'
+--
+-- gh-3466: Strange behaviour of tonumber64 function
+--
+tostring(tonumber64('18446744073709551615')) == '18446744073709551615ULL'
+tonumber64('18446744073709551616') == nil
+tostring(tonumber64('-9223372036854775808')) == '-9223372036854775808LL'

2. Where is a test for another corner case? I mean INT64_MAX.

+tonumber64('-9223372036854775809') == nil
+
  tonumber64('0x12') == 18
  tonumber64('0x12', 16) == 18
  tonumber64('0x12', 17) == nil


Other related posts: