[Tarantool-patches] [PATCH luajit 1/2] Fix tonumber("-0").

Igor Munkin imun at tarantool.org
Mon Jun 27 16:47:31 MSK 2022


Sergey,

Thanks for the patch! LGTM, with two nits, I've fixed on the branch by
myself.

On 26.01.22, Sergey Kaplun wrote:
> From: Mike Pall <mike>
> 
> Reported by bluecheetah001.
> 
> (cherry picked from 45a7e5073ce0a59465fef0b80bb08bd4e76b7979)
> 
> Common case for scanning a string containing a number is a string with a
> decimal number. For this case, a decimal number first casts to int32_t
> and later to double (lua_Number) if necessary. Information about sign of
> 0 is lost during the cast to int32_t. So resulting TValue contains `0.`
> instead of `-0.`.
> 
> This patch removes cast to int32_t for the case when resulting TValue
> contains double value.

Minor: I propose the following wording (it looks a bit more clear to me
at least):
| When an integral decimal number is given to `tonumber`, it is casted to
| int32_t at first and later to double (lua_Number) if necessary.
| Information about sign of 0 is lost during the cast to int32_t. So
| resulting TValue contains `0.` instead of `-0.`.
|
| This patch removes cast to int32_t for the cases when the input number
| can't be represented by an integer.

> 
> Sergey Kaplun:
> * added the description and the test for the problem
> 
> Part of tarantool/tarantool#6548
> ---
>  src/lj_strscan.c                              |  5 ++---
>  .../lj-528-tonumber-0.test.lua                | 19 +++++++++++++++++++
>  2 files changed, 21 insertions(+), 3 deletions(-)
>  create mode 100644 test/tarantool-tests/lj-528-tonumber-0.test.lua
> 

<snipped>

> diff --git a/test/tarantool-tests/lj-528-tonumber-0.test.lua b/test/tarantool-tests/lj-528-tonumber-0.test.lua
> new file mode 100644
> index 00000000..03ba2aff
> --- /dev/null
> +++ b/test/tarantool-tests/lj-528-tonumber-0.test.lua
> @@ -0,0 +1,19 @@
> +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"
> +)

Minor: Use single quotes instead of the double ones.

> +
> +-- Test file to demonstrate LuaJIT `tonumber('-0')` incorrect
> +-- behaviour.
> +-- See also https://github.com/LuaJIT/LuaJIT/issues/528.
> +local test = tap.test('lj-528-tonumber-0')
> +test:plan(1)
> +
> +-- As numbers `-0 == 0`, so convert it back to string.
> +test:ok(tostring(tonumber('-0')) == '-0', 'correct "-0" string parsing')
> +
> +os.exit(test:check() and 0 or 1)
> -- 
> 2.34.1
> 

-- 
Best regards,
IM


More information about the Tarantool-patches mailing list