Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED
Date: Wed, 21 Jul 2021 18:05:53 +0300	[thread overview]
Message-ID: <4f12b945f7600953437e2eb56a013bc59647757c.1626879871.git.imeevma@gmail.com> (raw)

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


             reply	other threads:[~2021-07-21 15:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-21 15:05 Mergen Imeev via Tarantool-patches [this message]
2021-07-26 19:41 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-27  7:41 Mergen Imeev via Tarantool-patches
2021-07-28  8:56 ` Timur Safin via Tarantool-patches
2021-07-28 16:24 ` Kirill Yukhin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4f12b945f7600953437e2eb56a013bc59647757c.1626879871.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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