Skip to content

Commit 1452199

Browse files
committed
MDEV-27336 Crash on DROP DATABASE due to out-of-bounds result from InnoDB SUBSTR()
eval_substr(): Do not allow the string buffer of the first argument to be extended. Trim the length of the returned result if it would exceed the end of the buffer.
1 parent 8e5f09a commit 1452199

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

mysql-test/suite/innodb/r/dropdb.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ create table `#mysql50#q.q` select 1;
1010
ERROR 42000: Incorrect table name '#mysql50#q.q'
1111
create table `#mysql50#q·q` select 1;
1212
drop database `b`;
13+
#
14+
# MDEV-27336 Crash on DROP DATABASE due to out-of-bounds result
15+
# from InnoDB SUBSTR() function
16+
#
17+
USE test;
18+
CREATE TABLE t1(a INT PRIMARY KEY) ENGINE=InnoDB;
19+
CREATE TABLE t2(a INT PRIMARY KEY REFERENCES t1(a)) ENGINE=InnoDB;
20+
CREATE DATABASE somewhat_longer_name_to_cause_trouble;
21+
DROP DATABASE somewhat_longer_name_to_cause_trouble;
22+
DROP TABLE t2,t1;

mysql-test/suite/innodb/t/dropdb.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ use `b`;
1414
create table `#mysql50#q.q` select 1;
1515
create table `#mysql50#q·q` select 1;
1616
drop database `b`;
17+
18+
--echo #
19+
--echo # MDEV-27336 Crash on DROP DATABASE due to out-of-bounds result
20+
--echo # from InnoDB SUBSTR() function
21+
--echo #
22+
USE test;
23+
CREATE TABLE t1(a INT PRIMARY KEY) ENGINE=InnoDB;
24+
CREATE TABLE t2(a INT PRIMARY KEY REFERENCES t1(a)) ENGINE=InnoDB;
25+
CREATE DATABASE somewhat_longer_name_to_cause_trouble;
26+
DROP DATABASE somewhat_longer_name_to_cause_trouble;
27+
DROP TABLE t2,t1;

storage/innobase/eval/eval0eval.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*****************************************************************************
22
33
Copyright (c) 1997, 2016, Oracle and/or its affiliates. All Rights Reserved.
4-
Copyright (c) 2019, MariaDB Corporation.
4+
Copyright (c) 2019, 2021, MariaDB Corporation.
55
66
This program is free software; you can redistribute it and/or modify it under
77
the terms of the GNU General Public License as published by the Free Software
@@ -378,12 +378,23 @@ eval_substr(
378378

379379
str1 = static_cast<byte*>(dfield_get_data(que_node_get_val(arg1)));
380380

381+
const ulint str1_len = dfield_get_len(que_node_get_val(arg1));
382+
381383
len1 = (ulint) eval_node_get_int_val(arg2);
382384
len2 = (ulint) eval_node_get_int_val(arg3);
383385

384386
dfield = que_node_get_val(func_node);
385387

386-
dfield_set_data(dfield, str1 + len1, len2);
388+
if (len1 > str1_len) {
389+
len2 = 0;
390+
} else {
391+
str1 += len1;
392+
if (len2 > str1_len - len1) {
393+
len2 = str1_len - len1;
394+
}
395+
}
396+
397+
dfield_set_data(dfield, str1, len2);
387398
}
388399

389400
/*****************************************************************//**

0 commit comments

Comments
 (0)