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

  • From: Alexander Turenko <alexander.turenko@xxxxxxxxxxxxx>
  • To: Vladislav Shpilevoy <v.shpilevoy@xxxxxxxxxxxxx>
  • Date: Mon, 16 Jul 2018 15:49:50 +0300

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.

Other related posts: