[Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED

imeevma at tarantool.org imeevma at tarantool.org
Tue Jul 27 10:41:37 MSK 2021


Prior to this patch it was possible to use CAST(<double> AS UNSIGNED)
and get negative result. For example, result of CAST(-1.5 AS UNSIGNED)
was -1. This patch removes this possibility. Now such cast can only
return UNSIGNED value.

Closes #6010
---
https://github.com/tarantool/tarantool/issues/6010
https://github.com/tarantool/tarantool/tree/imeevma/gh-6010-disallow-double-to-int-cast

 ...-6010-disallow-negative-double-to-unsigned-cast |  4 ++++
 src/box/sql/mem.c                                  |  2 +-
 test/sql-tap/numcast.test.lua                      | 14 +++++++++++++-
 test/sql/types.result                              |  7 ++-----
 4 files changed, 20 insertions(+), 7 deletions(-)
 create mode 100644 changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast

diff --git a/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
new file mode 100644
index 000000000..566be594f
--- /dev/null
+++ b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
@@ -0,0 +1,4 @@
+## bugfix/sql
+
+* Fixed explicit cast of negative DOUBLE to UNSIGNED, which could return
+negative result (gh-6010).
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index fa80f6d5a..e4ce233e0 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1061,7 +1061,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 		case MEM_TYPE_BIN:
 			return bytes_to_uint(mem);
 		case MEM_TYPE_DOUBLE:
-			return double_to_int(mem);
+			return double_to_uint(mem);
 		case MEM_TYPE_BOOL:
 			return bool_to_int(mem);
 		default:
diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua
index b3aa64e76..20aea3c4b 100755
--- a/test/sql-tap/numcast.test.lua
+++ b/test/sql-tap/numcast.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(31)
+test:plan(32)
 
 --!./tcltestrunner.lua
 -- 2013 March 20
@@ -249,4 +249,16 @@ test:do_execsql_test(
         0 , 0.33333333333333, 0.33333333333333
 })
 
+--
+-- gh-6010: Make sure that a negative DOUBLE value cannot be explicitly cast to
+-- UNSIGNED.
+--
+test:do_catchsql_test(
+    "numcast-4",
+    [[
+        SELECT CAST(-2.5 AS UNSIGNED);
+    ]], {
+        1, "Type mismatch: can not convert double(-2.5) to unsigned"
+})
+
 test:finish_test()
diff --git a/test/sql/types.result b/test/sql/types.result
index a0b8668be..c1e1a8ef1 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -1097,11 +1097,8 @@ box.execute("SELECT CAST(1.5 AS UNSIGNED);")
 ...
 box.execute("SELECT CAST(-1.5 AS UNSIGNED);")
 ---
-- metadata:
-  - name: COLUMN_1
-    type: unsigned
-  rows:
-  - [-1]
+- null
+- 'Type mismatch: can not convert double(-1.5) to unsigned'
 ...
 box.execute("SELECT CAST(true AS UNSIGNED);")
 ---
-- 
2.25.1



More information about the Tarantool-patches mailing list