Commit 841dc07e authored by Alexander Barkov's avatar Alexander Barkov

MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type...

MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself in my_strntoull_8bit on SELECT ... OCT

The code in my_strntoull_8bit() and my_strntoull_mb2_or_mb4()
could hit undefinite behavior by negating of LONGLONG_MIN.
Fixing the code to avoid this.
parent 0a5e4a01
...@@ -6540,5 +6540,14 @@ DROP VIEW v1; ...@@ -6540,5 +6540,14 @@ DROP VIEW v1;
DROP TABLE t1; DROP TABLE t1;
SET NAMES utf8mb3; SET NAMES utf8mb3;
# #
# MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself in my_strntoull_8bit on SELECT ... OCT
#
CREATE TABLE t1 (c TEXT CHARACTER SET ucs2);
INSERT INTO t1 VALUES ('-9223372036854775808.5');
SELECT OCT(c) FROM t1;
OCT(c)
1000000000000000000000
DROP TABLE t1;
#
# End of 10.5 tests # End of 10.5 tests
# #
...@@ -1217,6 +1217,15 @@ DROP VIEW v1; ...@@ -1217,6 +1217,15 @@ DROP VIEW v1;
DROP TABLE t1; DROP TABLE t1;
SET NAMES utf8mb3; SET NAMES utf8mb3;
--echo #
--echo # MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself in my_strntoull_8bit on SELECT ... OCT
--echo #
CREATE TABLE t1 (c TEXT CHARACTER SET ucs2);
INSERT INTO t1 VALUES ('-9223372036854775808.5');
SELECT OCT(c) FROM t1;
DROP TABLE t1;
--echo # --echo #
--echo # End of 10.5 tests --echo # End of 10.5 tests
......
...@@ -5317,5 +5317,18 @@ SELECT SUBSTR(0,@a) FROM t; ...@@ -5317,5 +5317,18 @@ SELECT SUBSTR(0,@a) FROM t;
SUBSTR(0,@a) SUBSTR(0,@a)
DROP TABLE t; DROP TABLE t;
# #
# MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself in my_strntoull_8bit on SELECT ... OCT
#
CREATE TABLE t1 (c BLOB);
INSERT INTO t1 VALUES ('-9223372036854775808.5');
SELECT OCT(c) FROM t1;
OCT(c)
1000000000000000000000
SELECT BIN(c) FROM t1;
BIN(c)
1000000000000000000000000000000000000000000000000000000000000000
DROP TABLE t1;
DO OCT(-9223372036854775808);
#
# End of 10.5 tests # End of 10.5 tests
# #
...@@ -2358,6 +2358,19 @@ CREATE TABLE t (c1 INT,c2 CHAR); ...@@ -2358,6 +2358,19 @@ CREATE TABLE t (c1 INT,c2 CHAR);
SELECT SUBSTR(0,@a) FROM t; SELECT SUBSTR(0,@a) FROM t;
DROP TABLE t; DROP TABLE t;
--echo #
--echo # MDEV-28386 UBSAN: runtime error: negation of -X cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself in my_strntoull_8bit on SELECT ... OCT
--echo #
CREATE TABLE t1 (c BLOB);
INSERT INTO t1 VALUES ('-9223372036854775808.5');
SELECT OCT(c) FROM t1;
SELECT BIN(c) FROM t1;
DROP TABLE t1;
DO OCT(-9223372036854775808);
--echo # --echo #
--echo # End of 10.5 tests --echo # End of 10.5 tests
--echo # --echo #
...@@ -758,7 +758,10 @@ ulonglong my_strntoull_8bit(CHARSET_INFO *cs, ...@@ -758,7 +758,10 @@ ulonglong my_strntoull_8bit(CHARSET_INFO *cs,
return (~(ulonglong) 0); return (~(ulonglong) 0);
} }
return (negative ? -((longlong) i) : (longlong) i); /* Avoid undefinite behavior - negation of LONGLONG_MIN */
return negative && (longlong) i != LONGLONG_MIN ?
-((longlong) i) :
(longlong) i;
noconv: noconv:
err[0]= EDOM; err[0]= EDOM;
......
...@@ -616,7 +616,10 @@ my_strntoull_mb2_or_mb4(CHARSET_INFO *cs, ...@@ -616,7 +616,10 @@ my_strntoull_mb2_or_mb4(CHARSET_INFO *cs,
return (~(ulonglong) 0); return (~(ulonglong) 0);
} }
return (negative ? -((longlong) res) : (longlong) res); /* Avoid undefinite behavior - negation of LONGLONG_MIN */
return negative && (longlong) res != LONGLONG_MIN ?
-((longlong) res) :
(longlong) res;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment