Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
@ 2018-07-13 11:21 Kirill Shcherbatov
  2018-07-16 10:23 ` [tarantool-patches] " Vladislav Shpilevoy
  2018-07-19 10:46 ` Kirill Yukhin
  0 siblings, 2 replies; 13+ messages in thread
From: Kirill Shcherbatov @ 2018-07-13 11:21 UTC (permalink / raw)
  To: tarantool-patches; +Cc: v.shpilevoy, Kirill Shcherbatov

Function tonumber64 has worked incorrectly with values less
than LLONG_MIN.
Now it works in the interval [LLONG_MIN, ULLONG_MAX] returning
nil otherwise.

Closes #3466.
---
Branch: https://github.com/tarantool/tarantool/compare/kshch/gh-3466-tonumber64-strange-behaviour
Issue: https://github.com/tarantool/tarantool/issues/3466

 src/lua/init.c         |  6 +++++-
 test/box/misc.result   | 19 +++++++++++++++++++
 test/box/misc.test.lua |  8 ++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/lua/init.c b/src/lua/init.c
index 9a96030..4b5285d 100644
--- a/src/lua/init.c
+++ b/src/lua/init.c
@@ -222,7 +222,11 @@ lbox_tonumber64(struct lua_State *L)
 			if (argl == 0) {
 				lua_pushnil(L);
 			} else if (negative) {
-				luaL_pushint64(L, -1 * (long long )result);
+				if (result > LLONG_MAX) {
+					lua_pushnil(L);
+				} else {
+					luaL_pushint64(L, -1 * result);
+				}
 			} else {
 				luaL_pushuint64(L, result);
 			}
diff --git a/test/box/misc.result b/test/box/misc.result
index f332a8c..dcd08d0 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('-9223372036854775807')) == '-9223372036854775807LL'
+---
+- true
+...
+tonumber64('-9223372036854775808') == nil
+---
+- true
+...
 tonumber64('0x12') == 18
 ---
 - true
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index e24228a..4816235 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('-9223372036854775807')) == '-9223372036854775807LL'
+tonumber64('-9223372036854775808') == nil
+
 tonumber64('0x12') == 18
 tonumber64('0x12', 16) == 18
 tonumber64('0x12', 17) == nil
-- 
2.7.4

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-13 11:21 [tarantool-patches] [PATCH v1 1/1] lua: fix strange behaviour of tonumber64 Kirill Shcherbatov
@ 2018-07-16 10:23 ` Vladislav Shpilevoy
  2018-07-16 12:49   ` Alexander Turenko
  2018-07-19 10:46 ` Kirill Yukhin
  1 sibling, 1 reply; 13+ messages in thread
From: Vladislav Shpilevoy @ 2018-07-16 10:23 UTC (permalink / raw)
  To: Kirill Shcherbatov, tarantool-patches

Thanks for the patch! See 4 comments below.

On 13/07/2018 14:21, Kirill Shcherbatov wrote:
> Function tonumber64 has worked incorrectly with values less
> than LLONG_MIN.
> Now it works in the interval [LLONG_MIN, ULLONG_MAX] returning
> nil otherwise.
> 
> Closes #3466.
> ---
> Branch: https://github.com/tarantool/tarantool/compare/kshch/gh-3466-tonumber64-strange-behaviour
> Issue: https://github.com/tarantool/tarantool/issues/3466
> 
>   src/lua/init.c         |  6 +++++-
>   test/box/misc.result   | 19 +++++++++++++++++++
>   test/box/misc.test.lua |  8 ++++++++
>   3 files changed, 32 insertions(+), 1 deletion(-)
> 
> diff --git a/src/lua/init.c b/src/lua/init.c
> index 9a96030..4b5285d 100644
> --- a/src/lua/init.c
> +++ b/src/lua/init.c
> @@ -222,7 +222,11 @@ lbox_tonumber64(struct lua_State *L)
>   			if (argl == 0) {
>   				lua_pushnil(L);
>   			} else if (negative) {
> -				luaL_pushint64(L, -1 * (long long )result);
> +				if (result > -((unsigned long long )LLONG_MIN)) {

1. Please, do not enclose one-line bodies into {}.

2. How can you cast LLONG_MIN (that is negative) to the unsigned type?

3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
it is not? (http://www.cplusplus.com/reference/climits/)

4. Why the function is named to64, but we use non-explicitly sized types?
I mean, why not to use uint64_t result, compare with INT64_MAX etc. According to
the C standard, LLONG_MAX is not restricted with 64 bits.

> +					lua_pushnil(L);
> +				} else {
> +					luaL_pushint64(L, -result);
> +				}
>   			} else {
>   				luaL_pushuint64(L, result);
>   			}

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 10:23 ` [tarantool-patches] " Vladislav Shpilevoy
@ 2018-07-16 12:49   ` Alexander Turenko
  2018-07-16 13:15     ` Vladislav Shpilevoy
  0 siblings, 1 reply; 13+ messages in thread
From: Alexander Turenko @ 2018-07-16 12:49 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: Kirill Shcherbatov, tarantool-patches

Hi, Vlad!

That is interesting discussion. Hope you don't mind my participation.

WBR, Alexander Turenko.

On Mon, Jul 16, 2018 at 01:23:36PM +0300, Vladislav Shpilevoy wrote:
> Thanks for the patch! See 4 comments below.
> 
> On 13/07/2018 14:21, Kirill Shcherbatov wrote:
> > Function tonumber64 has worked incorrectly with values less
> > than LLONG_MIN.
> > Now it works in the interval [LLONG_MIN, ULLONG_MAX] returning
> > nil otherwise.
> > 
> > Closes #3466.
> > ---
> > Branch: https://github.com/tarantool/tarantool/compare/kshch/gh-3466-tonumber64-strange-behaviour
> > Issue: https://github.com/tarantool/tarantool/issues/3466
> > 
> >   src/lua/init.c         |  6 +++++-
> >   test/box/misc.result   | 19 +++++++++++++++++++
> >   test/box/misc.test.lua |  8 ++++++++
> >   3 files changed, 32 insertions(+), 1 deletion(-)
> > 
> > diff --git a/src/lua/init.c b/src/lua/init.c
> > index 9a96030..4b5285d 100644
> > --- a/src/lua/init.c
> > +++ b/src/lua/init.c
> > @@ -222,7 +222,11 @@ lbox_tonumber64(struct lua_State *L)
> >   			if (argl == 0) {
> >   				lua_pushnil(L);
> >   			} else if (negative) {
> > -				luaL_pushint64(L, -1 * (long long )result);
> > +				if (result > -((unsigned long long )LLONG_MIN)) {
> 
> 1. Please, do not enclose one-line bodies into {}.
> 
> 2. How can you cast LLONG_MIN (that is negative) to the unsigned type?
> 

Cast does not change bits. It is legal.

> 3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
> it is not? (http://www.cplusplus.com/reference/climits/)
> 

No, LLONG_MAX is 2^63-1, but LLONG_MIN is -2^63. We want to compare
result with 2^63. We are trying to do so in platform-independent way
(hovewer unsiged unary nimus equivalence with signed one is likely
two-complement number representation property and can be violated on
other platforms).

Are you think we should introduce our own constant
9223372036854775808ULL (2^63) and avoid that complex assumptions set? It
would be explicitly number-representation-dependent, so maybe it is
better.

> 4. Why the function is named to64, but we use non-explicitly sized types?
> I mean, why not to use uint64_t result, compare with INT64_MAX etc. According to
> the C standard, LLONG_MAX is not restricted with 64 bits.
> 

Yep, it is better to use INT64_MIN from stdint.h.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 12:49   ` Alexander Turenko
@ 2018-07-16 13:15     ` Vladislav Shpilevoy
  2018-07-16 13:42       ` Alexander Turenko
  0 siblings, 1 reply; 13+ messages in thread
From: Vladislav Shpilevoy @ 2018-07-16 13:15 UTC (permalink / raw)
  To: Alexander Turenko; +Cc: Kirill Shcherbatov, tarantool-patches



On 16/07/2018 15:49, Alexander Turenko wrote:
> Hi, Vlad!
> 
> That is interesting discussion. Hope you don't mind my participation.

Hi! Your participation is appreciated!

> 
> WBR, Alexander Turenko.
> 
> On Mon, Jul 16, 2018 at 01:23:36PM +0300, Vladislav Shpilevoy wrote:
>> Thanks for the patch! See 4 comments below.
>>
>> On 13/07/2018 14:21, Kirill Shcherbatov wrote:
>>> Function tonumber64 has worked incorrectly with values less
>>> than LLONG_MIN.
>>> Now it works in the interval [LLONG_MIN, ULLONG_MAX] returning
>>> nil otherwise.
>>>
>>> Closes #3466.
>>> ---
>>> Branch: https://github.com/tarantool/tarantool/compare/kshch/gh-3466-tonumber64-strange-behaviour
>>> Issue: https://github.com/tarantool/tarantool/issues/3466
>>>
>>>    src/lua/init.c         |  6 +++++-
>>>    test/box/misc.result   | 19 +++++++++++++++++++
>>>    test/box/misc.test.lua |  8 ++++++++
>>>    3 files changed, 32 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/src/lua/init.c b/src/lua/init.c
>>> index 9a96030..4b5285d 100644
>>> --- a/src/lua/init.c
>>> +++ b/src/lua/init.c
>>> @@ -222,7 +222,11 @@ lbox_tonumber64(struct lua_State *L)
>>>    			if (argl == 0) {
>>>    				lua_pushnil(L);
>>>    			} else if (negative) {
>>> -				luaL_pushint64(L, -1 * (long long )result);
>>> +				if (result > -((unsigned long long )LLONG_MIN)) {
>>
>> 1. Please, do not enclose one-line bodies into {}.
>>
>> 2. How can you cast LLONG_MIN (that is negative) to the unsigned type?
>>
> 
> Cast does not change bits. It is legal.

Yes, technically it is legal, but casting negative value to an unsigned type
looks weird.

> 
>> 3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
>> it is not? (http://www.cplusplus.com/reference/climits/)
>>
> 
> No, LLONG_MAX is 2^63-1, but LLONG_MIN is -2^63. We want to compare
> result with 2^63. We are trying to do so in platform-independent way
> (hovewer unsiged unary nimus equivalence with signed one is likely
> two-complement number representation property and can be violated on
> other platforms).
> 
> Are you think we should introduce our own constant
> 9223372036854775808ULL (2^63) and avoid that complex assumptions set? It

Ultimately no. We should not invent the constants.

> would be explicitly number-representation-dependent, so maybe it is
> better.

Ok. Logically we want an error on -result < INT64_MIN, right?
It is the same as result > -INT64_MIN. But we can not say
-INT64_MIN because abs(INT64_MIN) > INT64_MAX, yes?

Then lets rephrase the comparison:

     result > -INT64_MIN
            |
            v
   result + 1 >= -INT64_MIN
            |
            v
     result >= -INT64_MIN - 1
            |
            v
    result >= -(INT64_MIN + 1) <- that is the solution.

As I understand, -(INT64_MIN + 1) is exactly 2^63 - 1 and
fits in int64, right?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 13:15     ` Vladislav Shpilevoy
@ 2018-07-16 13:42       ` Alexander Turenko
  2018-07-16 13:55         ` Vladislav Shpilevoy
  0 siblings, 1 reply; 13+ messages in thread
From: Alexander Turenko @ 2018-07-16 13:42 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: Kirill Shcherbatov, tarantool-patches

> > 
> > > 3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
> > > it is not? (http://www.cplusplus.com/reference/climits/)
> > > 
> > 
> > No, LLONG_MAX is 2^63-1, but LLONG_MIN is -2^63. We want to compare
> > result with 2^63. We are trying to do so in platform-independent way
> > (hovewer unsiged unary nimus equivalence with signed one is likely
> > two-complement number representation property and can be violated on
> > other platforms).
> > 
> > Are you think we should introduce our own constant
> > 9223372036854775808ULL (2^63) and avoid that complex assumptions set? It
> 
> Ultimately no. We should not invent the constants.
> 
> > would be explicitly number-representation-dependent, so maybe it is
> > better.
> 
> Ok. Logically we want an error on -result < INT64_MIN, right?
> It is the same as result > -INT64_MIN. But we can not say
> -INT64_MIN because abs(INT64_MIN) > INT64_MAX, yes?
> 

Yes.

> Then lets rephrase the comparison:
> 
>     result > -INT64_MIN
>            |
>            v
>   result + 1 >= -INT64_MIN
>            |
>            v
>     result >= -INT64_MIN - 1
>            |
>            v
>    result >= -(INT64_MIN + 1) <- that is the solution.
> 
> As I understand, -(INT64_MIN + 1) is exactly 2^63 - 1 and
> fits in int64, right?

2nd step should be result - 1 >= -INT64_MIN, so not it is not the
decision. Overflow is unavoidable while we are trying to operate within
the signed type.

WBR, Alexander Turenko.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 13:42       ` Alexander Turenko
@ 2018-07-16 13:55         ` Vladislav Shpilevoy
  2018-07-16 14:09           ` Alexander Turenko
  0 siblings, 1 reply; 13+ messages in thread
From: Vladislav Shpilevoy @ 2018-07-16 13:55 UTC (permalink / raw)
  To: Alexander Turenko; +Cc: Kirill Shcherbatov, tarantool-patches



On 16/07/2018 16:42, Alexander Turenko wrote:
>>>
>>>> 3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
>>>> it is not? (http://www.cplusplus.com/reference/climits/)
>>>>
>>>
>>> No, LLONG_MAX is 2^63-1, but LLONG_MIN is -2^63. We want to compare
>>> result with 2^63. We are trying to do so in platform-independent way
>>> (hovewer unsiged unary nimus equivalence with signed one is likely
>>> two-complement number representation property and can be violated on
>>> other platforms).
>>>
>>> Are you think we should introduce our own constant
>>> 9223372036854775808ULL (2^63) and avoid that complex assumptions set? It
>>
>> Ultimately no. We should not invent the constants.
>>
>>> would be explicitly number-representation-dependent, so maybe it is
>>> better.
>>
>> Ok. Logically we want an error on -result < INT64_MIN, right?
>> It is the same as result > -INT64_MIN. But we can not say
>> -INT64_MIN because abs(INT64_MIN) > INT64_MAX, yes?
>>
> 
> Yes.
> 
>> Then lets rephrase the comparison:
>>
>>      result > -INT64_MIN
>>             |
>>             v
>>    result + 1 >= -INT64_MIN
>>             |
>>             v
>>      result >= -INT64_MIN - 1
>>             |
>>             v
>>     result >= -(INT64_MIN + 1) <- that is the solution.
>>
>> As I understand, -(INT64_MIN + 1) is exactly 2^63 - 1 and
>> fits in int64, right?
> 
> 2nd step should be result - 1 >= -INT64_MIN, so not it is not the

Oh, stupid error, sorry.

> decision. Overflow is unavoidable while we are trying to operate within
> the signed type.

No, overflow is always avoidable. As an extreme solution we have int96 type,
that is already used for overflow checks on tuple update.

As a second one I again have tried to evolve my idea of reorganization of
result > -INT64_MIN expression:

     result > -INT64_MIN
            |
            v
  result - 1 > -INT64_MIN - 1
            |
            v
  INT64_MAX == -INT64_MIN - 1
            |
            v
    result - 1 > INT64_MAX

Here the result is uint64_t. So to check for overflow we use
this predicate:

result != 0 && result - 1 > INT64_MAX.

No type casts, no overflows, explicit sizes.
       

> 
> WBR, Alexander Turenko.
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 13:55         ` Vladislav Shpilevoy
@ 2018-07-16 14:09           ` Alexander Turenko
  2018-07-16 16:52             ` Kirill Shcherbatov
  0 siblings, 1 reply; 13+ messages in thread
From: Alexander Turenko @ 2018-07-16 14:09 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: Kirill Shcherbatov, tarantool-patches

On Mon, Jul 16, 2018 at 04:55:51PM +0300, Vladislav Shpilevoy wrote:
> 
> 
> On 16/07/2018 16:42, Alexander Turenko wrote:
> > > > 
> > > > > 3. Why not 'result > LLONG_MAX'? As I understand, abs(LLONG_MAX) == abs(LLONG_MIN),
> > > > > it is not? (http://www.cplusplus.com/reference/climits/)
> > > > > 
> > > > 
> > > > No, LLONG_MAX is 2^63-1, but LLONG_MIN is -2^63. We want to compare
> > > > result with 2^63. We are trying to do so in platform-independent way
> > > > (hovewer unsiged unary nimus equivalence with signed one is likely
> > > > two-complement number representation property and can be violated on
> > > > other platforms).
> > > > 
> > > > Are you think we should introduce our own constant
> > > > 9223372036854775808ULL (2^63) and avoid that complex assumptions set? It
> > > 
> > > Ultimately no. We should not invent the constants.
> > > 
> > > > would be explicitly number-representation-dependent, so maybe it is
> > > > better.
> > > 
> > > Ok. Logically we want an error on -result < INT64_MIN, right?
> > > It is the same as result > -INT64_MIN. But we can not say
> > > -INT64_MIN because abs(INT64_MIN) > INT64_MAX, yes?
> > > 
> > 
> > Yes.
> > 
> > > Then lets rephrase the comparison:
> > > 
> > >      result > -INT64_MIN
> > >             |
> > >             v
> > >    result + 1 >= -INT64_MIN
> > >             |
> > >             v
> > >      result >= -INT64_MIN - 1
> > >             |
> > >             v
> > >     result >= -(INT64_MIN + 1) <- that is the solution.
> > > 
> > > As I understand, -(INT64_MIN + 1) is exactly 2^63 - 1 and
> > > fits in int64, right?
> > 
> > 2nd step should be result - 1 >= -INT64_MIN, so not it is not the
> 
> Oh, stupid error, sorry.
> 
> > decision. Overflow is unavoidable while we are trying to operate within
> > the signed type.
> 
> No, overflow is always avoidable. As an extreme solution we have int96 type,
> that is already used for overflow checks on tuple update.
> 
> As a second one I again have tried to evolve my idea of reorganization of
> result > -INT64_MIN expression:
> 
>     result > -INT64_MIN
>            |
>            v
>  result - 1 > -INT64_MIN - 1
>            |
>            v
>  INT64_MAX == -INT64_MIN - 1
>            |
>            v
>    result - 1 > INT64_MAX
> 
> Here the result is uint64_t. So to check for overflow we use
> this predicate:
> 
> result != 0 && result - 1 > INT64_MAX.
> 
> No type casts, no overflows, explicit sizes.

Here we lean on assumption that INT64_MAX == -INT64_MIN - 1, but the
question was arisen because we trying to avoid that. At least it should
be properly commented.

I don't insist, but more like approach with explicit INT64_MIN usage.

By the way, result != 0 check is redundant, because (0ULL - 1) is
0xffffffffffffffff (unsigned value) and above than INT64_MAX.

WBR, Alexander Turenko.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 14:09           ` Alexander Turenko
@ 2018-07-16 16:52             ` Kirill Shcherbatov
  2018-07-17  9:35               ` Vladislav Shpilevoy
  0 siblings, 1 reply; 13+ messages in thread
From: Kirill Shcherbatov @ 2018-07-16 16:52 UTC (permalink / raw)
  To: tarantool-patches, Vladislav Shpilevoy

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)
+					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'
+tonumber64('-9223372036854775809') == nil
+
 tonumber64('0x12') == 18
 tonumber64('0x12', 16) == 18
 tonumber64('0x12', 17) == nil

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-16 16:52             ` Kirill Shcherbatov
@ 2018-07-17  9:35               ` Vladislav Shpilevoy
  2018-07-17 11:27                 ` Alexander Turenko
  0 siblings, 1 reply; 13+ messages in thread
From: Vladislav Shpilevoy @ 2018-07-17  9:35 UTC (permalink / raw)
  To: Kirill Shcherbatov, tarantool-patches

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
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-17  9:35               ` Vladislav Shpilevoy
@ 2018-07-17 11:27                 ` Alexander Turenko
  2018-07-17 12:03                   ` Kirill Shcherbatov
  0 siblings, 1 reply; 13+ messages in thread
From: Alexander Turenko @ 2018-07-17 11:27 UTC (permalink / raw)
  To: Kirill Shcherbatov; +Cc: Vladislav Shpilevoy, tarantool-patches

> > +-- 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.
> 

I also propose to test zero.

WBR, Alexander Turenko.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-17 11:27                 ` Alexander Turenko
@ 2018-07-17 12:03                   ` Kirill Shcherbatov
  2018-07-17 21:48                     ` Vladislav Shpilevoy
  0 siblings, 1 reply; 13+ messages in thread
From: Kirill Shcherbatov @ 2018-07-17 12:03 UTC (permalink / raw)
  To: tarantool-patches, Vladislav Shpilevoy, Alexander Turenko

>>> +-- gh-3466: Strange behaviour of tonumber64 function
>>> +--
>>> +tostring(tonumber64('18446744073709551615')) == '18446744073709551615ULL'
>>> +tonumber64('18446744073709551616') == nil
>> 2. Where is a test for another corner case? I mean INT64_MAX.
Actually, the max value is not INT64_MAX; but UINT64_MAX that is tested above.
But I don't mind to add INT64_MAX test.
+tostring(tonumber64('9223372036854775807')) == '9223372036854775807ULL'

> I also propose to test zero.
+tostring(tonumber64('0')) == '0'

> 1. Please, add a comment about the things we have discussed as Alexander asked
> in the previous message.
+                               /*
+                                * To test overflow, consider
+                                *  result > -INT64_MIN;
+                                *  result - 1 > -INT64_MIN - 1;
+                                * Assumption:
+                                *  INT64_MAX == -(INT64_MIN + 1);
+                                * Finally,
+                                *  result - 1 > INT64_MAX;
+                                */

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-17 12:03                   ` Kirill Shcherbatov
@ 2018-07-17 21:48                     ` Vladislav Shpilevoy
  0 siblings, 0 replies; 13+ messages in thread
From: Vladislav Shpilevoy @ 2018-07-17 21:48 UTC (permalink / raw)
  To: Kirill Shcherbatov, tarantool-patches, Alexander Turenko

Thanks for the fixes! I have fixed the commit
message on the branch. Now the patch LGTM.

On 17/07/2018 15:03, Kirill Shcherbatov wrote:
>>>> +-- gh-3466: Strange behaviour of tonumber64 function
>>>> +--
>>>> +tostring(tonumber64('18446744073709551615')) == '18446744073709551615ULL'
>>>> +tonumber64('18446744073709551616') == nil
>>> 2. Where is a test for another corner case? I mean INT64_MAX.
> Actually, the max value is not INT64_MAX; but UINT64_MAX that is tested above.
> But I don't mind to add INT64_MAX test.
> +tostring(tonumber64('9223372036854775807')) == '9223372036854775807ULL'
> 
>> I also propose to test zero.
> +tostring(tonumber64('0')) == '0'
> 
>> 1. Please, add a comment about the things we have discussed as Alexander asked
>> in the previous message.
> +                               /*
> +                                * To test overflow, consider
> +                                *  result > -INT64_MIN;
> +                                *  result - 1 > -INT64_MIN - 1;
> +                                * Assumption:
> +                                *  INT64_MAX == -(INT64_MIN + 1);
> +                                * Finally,
> +                                *  result - 1 > INT64_MAX;
> +                                */
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] lua: fix strange behaviour of tonumber64
  2018-07-13 11:21 [tarantool-patches] [PATCH v1 1/1] lua: fix strange behaviour of tonumber64 Kirill Shcherbatov
  2018-07-16 10:23 ` [tarantool-patches] " Vladislav Shpilevoy
@ 2018-07-19 10:46 ` Kirill Yukhin
  1 sibling, 0 replies; 13+ messages in thread
From: Kirill Yukhin @ 2018-07-19 10:46 UTC (permalink / raw)
  To: tarantool-patches; +Cc: v.shpilevoy, Kirill Shcherbatov

Hello,
On 13 июл 14:21, Kirill Shcherbatov wrote:
> Function tonumber64 has worked incorrectly with values less
> than LLONG_MIN.
> Now it works in the interval [LLONG_MIN, ULLONG_MAX] returning
> nil otherwise.
> 
> Closes #3466.
> ---
> Branch: https://github.com/tarantool/tarantool/compare/kshch/gh-3466-tonumber64-strange-behaviour
> Issue: https://github.com/tarantool/tarantool/issues/3466
I've checked the patch into 1-10 branch.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-07-19 10:46 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-13 11:21 [tarantool-patches] [PATCH v1 1/1] lua: fix strange behaviour of tonumber64 Kirill Shcherbatov
2018-07-16 10:23 ` [tarantool-patches] " Vladislav Shpilevoy
2018-07-16 12:49   ` Alexander Turenko
2018-07-16 13:15     ` Vladislav Shpilevoy
2018-07-16 13:42       ` Alexander Turenko
2018-07-16 13:55         ` Vladislav Shpilevoy
2018-07-16 14:09           ` Alexander Turenko
2018-07-16 16:52             ` Kirill Shcherbatov
2018-07-17  9:35               ` Vladislav Shpilevoy
2018-07-17 11:27                 ` Alexander Turenko
2018-07-17 12:03                   ` Kirill Shcherbatov
2018-07-17 21:48                     ` Vladislav Shpilevoy
2018-07-19 10:46 ` Kirill Yukhin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox