Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 2/4] sql: raise an integer overflow error during CAST
Date: Wed, 20 Feb 2019 14:57:38 +0300	[thread overview]
Message-ID: <4d0d34db733fd778d4aa9c4128df95b5fdc917de.1550663540.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1550663540.git.korablev@tarantool.org>
In-Reply-To: <cover.1550663540.git.korablev@tarantool.org>

Before this patch, if integer overflow occurred during casting to
integer, it was simply ignored. As a result, wrong results might take
place. Lets check possible overflows before CAST conversion, and if it
happens, raise an appropriate error.

Part of #3735
---
 src/box/sql/vdbemem.c              |  4 +++-
 test/sql/integer-overflow.result   | 16 ++++++++++++++++
 test/sql/integer-overflow.test.lua | 10 ++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index e9ace33d2..a816936f0 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -532,7 +532,9 @@ sqlVdbeMemIntegerify(Mem * pMem, bool is_forced)
 		MemSetTypeFlag(pMem, MEM_Int);
 		return 0;
 	} else if ((pMem->flags & MEM_Real) != 0 && is_forced) {
-		pMem->u.i = (int) pMem->u.r;
+		if (pMem->u.r >= INT64_MAX || pMem->u.r < INT64_MIN)
+			return -1;
+		pMem->u.i = (int64_t) pMem->u.r;
 		MemSetTypeFlag(pMem, MEM_Int);
 		return 0;
 	}
diff --git a/test/sql/integer-overflow.result b/test/sql/integer-overflow.result
index fedcd4290..762ebbf29 100644
--- a/test/sql/integer-overflow.result
+++ b/test/sql/integer-overflow.result
@@ -40,3 +40,19 @@ box.sql.execute('SELECT 9223372036854775808 - 1;')
 ---
 - error: 'oversized integer: 9223372036854775808'
 ...
+-- Test that CAST may also leads to overflow.
+--
+box.sql.execute('SELECT CAST(\'9223372036854775808\' AS INTEGER);')
+---
+- error: 'Type mismatch: can not convert 9223372036854775808 to integer'
+...
+-- Due to inexact represantation of large integers in terms of
+-- floating point numbers, numerics with value < INT64_MAX
+-- have INT64_MAX + 1 value in integer representation:
+-- float 9223372036854775800 -> int (9223372036854775808),
+-- with error due to conversion = 8.
+--
+box.sql.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);')
+---
+- error: 'Type mismatch: can not convert 9.22337203685478e+18 to integer'
+...
diff --git a/test/sql/integer-overflow.test.lua b/test/sql/integer-overflow.test.lua
index 49ba5cd8f..ec7eb433e 100644
--- a/test/sql/integer-overflow.test.lua
+++ b/test/sql/integer-overflow.test.lua
@@ -14,3 +14,13 @@ box.sql.execute('SELECT (9223372036854775807 + 1);')
 box.sql.execute('SELECT 9223372036854775808;')
 box.sql.execute('SELECT -9223372036854775809;')
 box.sql.execute('SELECT 9223372036854775808 - 1;')
+-- Test that CAST may also leads to overflow.
+--
+box.sql.execute('SELECT CAST(\'9223372036854775808\' AS INTEGER);')
+-- Due to inexact represantation of large integers in terms of
+-- floating point numbers, numerics with value < INT64_MAX
+-- have INT64_MAX + 1 value in integer representation:
+-- float 9223372036854775800 -> int (9223372036854775808),
+-- with error due to conversion = 8.
+--
+box.sql.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);')
-- 
2.15.1

  parent reply	other threads:[~2019-02-20 11:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-20 11:57 [tarantool-patches] [PATCH 0/4] Fix integer overflow behaviour during VDBE execution Nikita Pettik
2019-02-20 11:57 ` [tarantool-patches] [PATCH 1/4] sql: raise an error if int is overflowed during math operations Nikita Pettik
2019-02-20 11:57 ` Nikita Pettik [this message]
2019-02-20 11:57 ` [tarantool-patches] [PATCH 3/4] sql: refactor sqlVdbeMsgpackGet() Nikita Pettik
2019-02-20 11:57 ` [tarantool-patches] [PATCH 4/4] sql: raise integer overflow error during msgpack decode Nikita Pettik
2019-02-20 18:25   ` [tarantool-patches] " Konstantin Osipov
2019-02-20 18:39     ` n.pettik
2019-02-20 18:46       ` Konstantin Osipov
2019-02-22 18:30 ` [tarantool-patches] Re: [PATCH 0/4] Fix integer overflow behaviour during VDBE execution Vladislav Shpilevoy
2019-02-25 11:58 ` Kirill Yukhin

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=4d0d34db733fd778d4aa9c4128df95b5fdc917de.1550663540.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH 2/4] sql: raise an integer overflow error during CAST' \
    /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