[Tarantool-patches] [PATCH luajit 2/2] Fix tonumber("-0") in dual-number mode.

Sergey Kaplun skaplun at tarantool.org
Wed Jan 26 15:19:34 MSK 2022


From: Mike Pall <mike>

Reported by Sergey Kaplun.

For DUALNUM build `tonumber("-0x0")` or `tonumber("-0")` returns 0
instead -0 due to the STRSCAN_OPT_TOINT option of `lj_strscan_scan()`
and cast value to integer with losing information about its sign.

This patch adds special checks for these corner cases during strscan.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#6548
---
 src/lj_strscan.c                                |  8 ++++++--
 test/tarantool-tests/lj-528-tonumber-0.test.lua | 10 ++--------
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/lj_strscan.c b/src/lj_strscan.c
index 4e4ef6d3..11d341ee 100644
--- a/src/lj_strscan.c
+++ b/src/lj_strscan.c
@@ -121,7 +121,8 @@ static StrScanFmt strscan_hex(const uint8_t *p, TValue *o,
   /* Format-specific handling. */
   switch (fmt) {
   case STRSCAN_INT:
-    if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) {
+    if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg &&
+	!(x == 0 && neg)) {
       o->i = neg ? -(int32_t)x : (int32_t)x;
       return STRSCAN_INT;  /* Fast path for 32 bit integers. */
     }
@@ -498,6 +499,9 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o,
       if ((opt & STRSCAN_OPT_TONUM)) {
 	o->n = neg ? -(double)x : (double)x;
 	return STRSCAN_NUM;
+      } else if (x == 0 && neg) {
+	o->n = -0.0;
+	return STRSCAN_NUM;
       } else {
 	o->i = neg ? -(int32_t)x : (int32_t)x;
 	return STRSCAN_INT;
@@ -515,7 +519,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o,
       fmt = strscan_dec(sp, o, fmt, opt, ex, neg, dig);
 
     /* Try to convert number to integer, if requested. */
-    if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT)) {
+    if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT) && !tvismzero(o)) {
       double n = o->n;
       int32_t i = lj_num2int(n);
       if (n == (lua_Number)i) { o->i = i; return STRSCAN_INT; }
diff --git a/test/tarantool-tests/lj-528-tonumber-0.test.lua b/test/tarantool-tests/lj-528-tonumber-0.test.lua
index 03ba2aff..f3ad53d3 100644
--- a/test/tarantool-tests/lj-528-tonumber-0.test.lua
+++ b/test/tarantool-tests/lj-528-tonumber-0.test.lua
@@ -1,15 +1,9 @@
 local tap = require('tap')
 
--- Test disabled for DUALNUM mode default for some arches.
--- See also https://github.com/LuaJIT/LuaJIT/pull/787.
-require("utils").skipcond(
-  jit.arch ~= "x86" and jit.arch ~= "x64",
-  jit.arch.." in DUALNUM mode is clumsy for now"
-)
-
 -- Test file to demonstrate LuaJIT `tonumber('-0')` incorrect
 -- behaviour.
--- See also https://github.com/LuaJIT/LuaJIT/issues/528.
+-- See also https://github.com/LuaJIT/LuaJIT/issues/528,
+-- https://github.com/LuaJIT/LuaJIT/pull/787.
 local test = tap.test('lj-528-tonumber-0')
 test:plan(1)
 
-- 
2.34.1



More information about the Tarantool-patches mailing list