Bug #121973 String exponent 2^32+k is stored into INT/BIGINT as exponent k, without an error

Submitted: 29 Nov 4:56
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Data Types Severity:S3 (Non-critical)
Version:26.10.0.1 (trunk 4573da3722), 9.7.4, 8.4 OS:Any ( Linux (official Docker images, and a source build on Ubuntu 24.04, x86_64). Not OS-specific.)
Assigned to: CPU Architecture:Any

[29 Nov 4:56] Ke Han
Description:
A numeric string with an exponent is stored into an INT or BIGINT column
with the exponent taken modulo 2^32.  In strict mode, '1e20' is rejected
with ERROR 1264 (Out of range value), but '1e4294967296' (exponent 2^32) is
stored as 1, '1e4294967297' as 10, and '-1234.1e4294967298' into an INT
column as -123410, the value of '-1234.1e2'.  A tiny value is made larger:
'7e-4294967296' is stored as 7, where '7e-20' is stored as 0.  An exponent
of 2^31 or more turns into a negative shift: '-1234.1e2147483649' is stored
as 0.  There is no error and no warning, also with INSERT IGNORE (which
clamps '1e20' to 9223372036854775807 with warning 1264), with UPDATE, and
for a utf32 string.

The string-to-integer conversion of Field_long/Field_longlong::store() is
my_strntoull10rnd_8bit() (utf32/ucs2/utf16 strings reach it through
my_strntoull10rnd_mb2_or_mb4()).  It accumulates the exponent in a long
long and checks it only against LLONG_MAX, then adds it to the int shift:

strings/ctype-simple.cc, my_strntoull10rnd_8bit() (trunk 4573da3722)

  1222  int shift = 0;
  ...
  1325      long long negative_exp = 0;
  1326      long long exponent = 0;
  ...
  1330      for (exponent = 0; str < end && (ch = (uint8_t)(*str - '0')) < 10;
  1331           str++) {
  1332        if (exponent <= (std::numeric_limits<long long>::max() - ch) / 10)
  1333          exponent = exponent * 10 + ch;
  1334        else
  1335          goto ret_too_big;
  1336      }
  1337      shift += negative_exp ? -exponent : exponent;

The assignment to the int shift keeps the low 32 bits of the exponent, and
the later range checks (shift > DIGITS_IN_ULONGLONG -> too big; -shift >=
DIGITS_IN_ULONGLONG -> zero) see the cut value.

How to repeat:
SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
CREATE DATABASE x;
USE x;
CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, a INT, b BIGINT);

INSERT INTO t (b) VALUES ('1e20');
-- ERROR 1264 (22003): Out of range value for column 'b' at row 1
INSERT INTO t (a) VALUES ('-1234.1e9');
-- ERROR 1264 (22003): Out of range value for column 'a' at row 1

INSERT INTO t (a) VALUES ('-1234.1e4294967298');   -- Query OK
INSERT INTO t (a) VALUES ('-1234.1e2147483649');   -- Query OK
INSERT INTO t (b) VALUES ('1e4294967296');         -- Query OK
INSERT INTO t (b) VALUES ('1e4294967297');         -- Query OK
INSERT INTO t (b) VALUES ('7e-20');                -- Query OK
INSERT INTO t (b) VALUES ('7e-4294967296');        -- Query OK
INSERT INTO t (b) VALUES (CONVERT('1e4294967297' USING utf32));  -- Query OK
INSERT IGNORE INTO t (b) VALUES ('1e4294967297');  -- Query OK, no warning
SELECT id, a, b FROM t ORDER BY id;
-- 1  -123410  NULL
-- 2        0  NULL
-- 3     NULL     1
-- 4     NULL    10
-- 5     NULL     0
-- 6     NULL     7
-- 7     NULL    10
-- 8     NULL    10

UPDATE t SET b = '5e4294967297' WHERE id = 3;      -- Query OK, no warning
SELECT b FROM t WHERE id = 3;                       -- 50

Expected: ERROR 1264 for rows 1-4, 7 and the UPDATE (as for '1e20' and
'-1234.1e9'), 0 for '7e-4294967296' (as for '7e-20'), and for INSERT IGNORE
the clamped value 9223372036854775807 with warning 1264 (as for '1e20').

All answers are from 26.10.0.1 (trunk 4573da3722); 9.7.4 and 8.4.13 give
the same.

Suggested fix:
Bound the exponent while it is read, before it is added to the int shift:
stop (ret_too_big for a positive exponent, ret_zero for a negative one) as
soon as exponent + shift (or exponent - shift) exceeds DIGITS_IN_ULONGLONG,
or check the long long sum against the int range before assigning it.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.041 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.