1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

MDEV-25704 RANDOM_BYTES - post-review fixes

don't error out on values out of range, return NULL
(as in, e.g. sqrt(-1))
This commit is contained in:
Sergei Golubchik
2022-07-27 17:05:51 +02:00
parent d7e3265dd3
commit 3607da3c4e
3 changed files with 59 additions and 43 deletions

View File

@@ -5279,9 +5279,14 @@ drop table t1;
select count(*) from seq_17_to_1024 where random_bytes(seq) <=> random_bytes(seq); select count(*) from seq_17_to_1024 where random_bytes(seq) <=> random_bytes(seq);
count(*) count(*)
0 0
select (count(*) = 1024) from seq_1_to_1024 where length(random_bytes(seq)) = seq; select count(*) from seq_1_to_1024 where length(random_bytes(seq)) = seq;
(count(*) = 1024) count(*)
1 1024
select random_bytes(`0`),`1` from (values (0,1),(null,2),(0,3)) t1;
random_bytes(`0`) 1
1
NULL 2
3
# #
# Test NULL output for NULL input # Test NULL output for NULL input
# #
@@ -5289,42 +5294,68 @@ SELECT random_bytes(NULL);
random_bytes(NULL) random_bytes(NULL)
NULL NULL
# #
# Test For values outside range from 1 to 1024, an error occurs # Test For values outside range from 1 to 1024 return NULL
# #
SELECT random_bytes(0); SELECT random_bytes(0);
ERROR 22003: length value is out of range in 'random_bytes(0)' random_bytes(0)
SELECT random_bytes(-1); SELECT random_bytes(-1);
ERROR 22003: length value is out of range in 'random_bytes(-1)' random_bytes(-1)
NULL
SELECT random_bytes(-100); SELECT random_bytes(-100);
ERROR 22003: length value is out of range in 'random_bytes(-100)' random_bytes(-100)
NULL
SELECT random_bytes(-26); SELECT random_bytes(-26);
ERROR 22003: length value is out of range in 'random_bytes(-26)' random_bytes(-26)
NULL
SELECT random_bytes(-132); SELECT random_bytes(-132);
ERROR 22003: length value is out of range in 'random_bytes(-132)' random_bytes(-132)
NULL
SELECT random_bytes(1025); SELECT random_bytes(1025);
ERROR 22003: length value is out of range in 'random_bytes(1025)' random_bytes(1025)
NULL
SELECT random_bytes(11111); SELECT random_bytes(11111);
ERROR 22003: length value is out of range in 'random_bytes(11111)' random_bytes(11111)
NULL
SELECT random_bytes(2056); SELECT random_bytes(2056);
ERROR 22003: length value is out of range in 'random_bytes(2056)' random_bytes(2056)
NULL
# #
# Test For invalid argument, an error occurs # Test For invalid argument return NULL
# #
SELECT random_bytes('s'); SELECT random_bytes('s');
ERROR 22003: length value is out of range in 'random_bytes('s')' random_bytes('s')
Warnings:
Warning 1292 Truncated incorrect INTEGER value: 's'
Warning 1292 Truncated incorrect INTEGER value: 's'
SELECT random_bytes('r'); SELECT random_bytes('r');
ERROR 22003: length value is out of range in 'random_bytes('r')' random_bytes('r')
Warnings:
Warning 1292 Truncated incorrect INTEGER value: 'r'
Warning 1292 Truncated incorrect INTEGER value: 'r'
SELECT random_bytes('res'); SELECT random_bytes('res');
ERROR 22003: length value is out of range in 'random_bytes('res')' random_bytes('res')
Warnings:
Warning 1292 Truncated incorrect INTEGER value: 'res'
Warning 1292 Truncated incorrect INTEGER value: 'res'
SELECT random_bytes('test'); SELECT random_bytes('test');
ERROR 22003: length value is out of range in 'random_bytes('test')' random_bytes('test')
Warnings:
Warning 1292 Truncated incorrect INTEGER value: 'test'
Warning 1292 Truncated incorrect INTEGER value: 'test'
# #
# MDEV-29108: RANDOM_BYTES - assertion in Create_tmp_table::finalize # MDEV-29108: RANDOM_BYTES - assertion in Create_tmp_table::finalize
# #
CREATE TABLE t (a INT); CREATE TABLE t (a INT);
INSERT INTO t VALUES (1),(2); INSERT INTO t VALUES (1),(2);
SELECT RANDOM_BYTES(-10) f1, IFNULL(a,1) f2 FROM t GROUP BY f1, f2; SELECT RANDOM_BYTES(-10) f1, IFNULL(a,1) f2 FROM t GROUP BY f1, f2;
ERROR 22003: length value is out of range in 'random_bytes(-10)' f1 f2
NULL 1
NULL 2
DROP TABLE t; DROP TABLE t;
# #
# MDEV-29154: Excessive warnings upon a call to RANDOM_BYTES # MDEV-29154: Excessive warnings upon a call to RANDOM_BYTES

View File

@@ -2206,7 +2206,9 @@ drop table t1;
--echo # The sequence starts at 17 so that the probability of test failure is small enough (about 2^(-136)) --echo # The sequence starts at 17 so that the probability of test failure is small enough (about 2^(-136))
select count(*) from seq_17_to_1024 where random_bytes(seq) <=> random_bytes(seq); select count(*) from seq_17_to_1024 where random_bytes(seq) <=> random_bytes(seq);
select (count(*) = 1024) from seq_1_to_1024 where length(random_bytes(seq)) = seq; select count(*) from seq_1_to_1024 where length(random_bytes(seq)) = seq;
select random_bytes(`0`),`1` from (values (0,1),(null,2),(0,3)) t1;
--echo # --echo #
--echo # Test NULL output for NULL input --echo # Test NULL output for NULL input
@@ -2215,36 +2217,24 @@ select (count(*) = 1024) from seq_1_to_1024 where length(random_bytes(seq)) = se
SELECT random_bytes(NULL); SELECT random_bytes(NULL);
--echo # --echo #
--echo # Test For values outside range from 1 to 1024, an error occurs --echo # Test For values outside range from 1 to 1024 return NULL
--echo # --echo #
--error 1690
SELECT random_bytes(0); SELECT random_bytes(0);
--error 1690
SELECT random_bytes(-1); SELECT random_bytes(-1);
--error 1690
SELECT random_bytes(-100); SELECT random_bytes(-100);
--error 1690
SELECT random_bytes(-26); SELECT random_bytes(-26);
--error 1690
SELECT random_bytes(-132); SELECT random_bytes(-132);
--error 1690
SELECT random_bytes(1025); SELECT random_bytes(1025);
--error 1690
SELECT random_bytes(11111); SELECT random_bytes(11111);
--error 1690
SELECT random_bytes(2056); SELECT random_bytes(2056);
--echo # --echo #
--echo # Test For invalid argument, an error occurs --echo # Test For invalid argument return NULL
--echo # --echo #
--error 1690
SELECT random_bytes('s'); SELECT random_bytes('s');
--error 1690
SELECT random_bytes('r'); SELECT random_bytes('r');
--error 1690
SELECT random_bytes('res'); SELECT random_bytes('res');
--error 1690
SELECT random_bytes('test'); SELECT random_bytes('test');
--echo # --echo #
@@ -2253,7 +2243,6 @@ SELECT random_bytes('test');
CREATE TABLE t (a INT); CREATE TABLE t (a INT);
INSERT INTO t VALUES (1),(2); INSERT INTO t VALUES (1),(2);
--error 1690
SELECT RANDOM_BYTES(-10) f1, IFNULL(a,1) f2 FROM t GROUP BY f1, f2; SELECT RANDOM_BYTES(-10) f1, IFNULL(a,1) f2 FROM t GROUP BY f1, f2;
# Cleanup # Cleanup

View File

@@ -1507,15 +1507,11 @@ String *Item_func_random_bytes::val_str(String *str)
goto err; goto err;
null_value= 0; null_value= 0;
if (count < 1 || count > MAX_RANDOM_BYTES) if (count < 0 || count > MAX_RANDOM_BYTES)
{ goto err;
char buf[256];
String msg(buf, sizeof(buf), system_charset_info); if (count == 0)
msg.length(0);
print(&msg, QT_NO_DATA_EXPANSION);
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "length", msg.c_ptr_safe());
return make_empty_result(str); return make_empty_result(str);
}
if (str->alloc((uint) count)) if (str->alloc((uint) count))
goto err; goto err;
@@ -1530,7 +1526,7 @@ String *Item_func_random_bytes::val_str(String *str)
ERR_error_string_n(ssl_err, buf, sizeof(buf)); ERR_error_string_n(ssl_err, buf, sizeof(buf));
sql_print_warning("SSL error: %s", buf); sql_print_warning("SSL error: %s", buf);
} }
return make_empty_result(str); goto err;
} }
return str; return str;