From 694db99d6193ae9975468617e98c165d24696cd7 Mon Sep 17 00:00:00 2001
From: Dmitry Simonenko <pmwkaa@gmail.com>
Date: Thu, 21 Feb 2013 17:13:19 +0400
Subject: [PATCH] bugfix1131108: tonumber64 from negative int inconsistency
 https://bugs.launchpad.net/tarantool/+bug/1131108

---
 .gitignore          |  7 +++++++
 src/lua/init.m      |  8 ++++++--
 test/box/lua.result | 33 +++++++++++++++++++++++++++++++++
 test/box/lua.test   |  9 +++++++++
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index dd10f178d0..f361e10b8a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -70,3 +70,10 @@ connector/c/tntsql/libtarantoolsql.so.1
 connector/c/tntsql/libtarantoolsql.so.1.1
 connector/c/tntrpl/libtarantoolrpl.so.1
 connector/c/tntrpl/libtarantoolrpl.so.1.1
+src/box/00000000000000000001.snap
+src/box/tarantool.cfg
+test/connector_c/tp
+test/unit/bit_test
+test/unit/bitset_basic_test
+test/unit/bitset_index_test
+test/unit/bitset_iterator_test
diff --git a/src/lua/init.m b/src/lua/init.m
index 1afd9ba0ea..2e8a285814 100644
--- a/src/lua/init.m
+++ b/src/lua/init.m
@@ -837,9 +837,11 @@ tarantool_lua_printstack_yaml(struct lua_State *L, struct tbuf *out)
 	int top = lua_gettop(L);
 	for (int i = 1; i <= top; i++) {
 		if (lua_type(L, i) == LUA_TCDATA) {
+			GCcdata *cd = cdataV(L->base + i - 1);
 			const char *sz = tarantool_lua_tostring(L, i);
 			int len = strlen(sz);
-			tbuf_printf(out, " - %-.*s" CRLF, len - 3, sz);
+			int chop = (cd->typeid == CTID_UINT64 ? 3 : 2);
+			tbuf_printf(out, " - %-.*s" CRLF, len - chop, sz);
 		} else
 			tbuf_printf(out, " - %s" CRLF,
 				    tarantool_lua_tostring(L, i));
@@ -856,9 +858,11 @@ tarantool_lua_printstack(struct lua_State *L, struct tbuf *out)
 	int top = lua_gettop(L);
 	for (int i = 1; i <= top; i++) {
 		if (lua_type(L, i) == LUA_TCDATA) {
+			GCcdata *cd = cdataV(L->base + i - 1);
 			const char *sz = tarantool_lua_tostring(L, i);
 			int len = strlen(sz);
-			tbuf_printf(out, "%-.*s" CRLF, len - 3, sz);
+			int chop = (cd->typeid == CTID_UINT64 ? 3 : 2);
+			tbuf_printf(out, "%-.*s" CRLF, len - chop, sz);
 		} else
 			tbuf_printf(out, "%s", tarantool_lua_tostring(L, i));
 	}
diff --git a/test/box/lua.result b/test/box/lua.result
index 5ee7f36e84..c5fe615024 100644
--- a/test/box/lua.result
+++ b/test/box/lua.result
@@ -1783,3 +1783,36 @@ lua t[2], t[3], t[4], t[5]
 lua box.space[0]:truncate()
 ---
 ...
+# A test case for Bug#1131108 'tonumber64 from negative int inconsistency'
+lua tonumber64(-1)
+---
+ - 18446744073709551615
+...
+lua tonumber64(-1LL)
+---
+ - 18446744073709551615
+...
+lua tonumber64(-1ULL)
+---
+ - 18446744073709551615
+...
+lua -1
+---
+ - -1
+...
+lua -1LL
+---
+ - -1
+...
+lua -1ULL
+---
+ - 18446744073709551615
+...
+lua tonumber64(-1.0)
+---
+ - 18446744073709551615
+...
+lua 6LL - 7LL
+---
+ - -1
+...
diff --git a/test/box/lua.test b/test/box/lua.test
index 3f7f9851f5..c7d725dfca 100644
--- a/test/box/lua.test
+++ b/test/box/lua.test
@@ -593,3 +593,12 @@ print """# A test case for tuple:totable() method"""
 exec admin "lua t=box.select(0, 0, 777):totable()"
 exec admin "lua t[2], t[3], t[4], t[5]"
 exec admin "lua box.space[0]:truncate()"
+print """# A test case for Bug#1131108 'tonumber64 from negative int inconsistency'"""
+exec admin "lua tonumber64(-1)"
+exec admin "lua tonumber64(-1LL)"
+exec admin "lua tonumber64(-1ULL)"
+exec admin "lua -1"
+exec admin "lua -1LL"
+exec admin "lua -1ULL"
+exec admin "lua tonumber64(-1.0)"
+exec admin "lua 6LL - 7LL"
-- 
GitLab