Skip to content
Snippets Groups Projects
Commit 95ca0bd7 authored by Nikita Pettik's avatar Nikita Pettik Committed by Kirill Yukhin
Browse files

sql: raise an integer overflow error during CAST

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
parent 53a8ba87
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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'
...
......@@ -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);')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment