Skip to content

Commit bdf32eb

Browse files
committed
MDEV-38474: ASAN heap-use-after-free in st_join_table::cleanup
Fix a regression introduced by 34a8209 which added cleanup_stranded_units() to the start of st_select_lex_unit::cleanup(). Moving cleanup_stranded_units() to the end of the cleanup() function ensures a parent-first cleanup order, which is required for the safe destruction of merged tables.
1 parent a6e9876 commit bdf32eb

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

mysql-test/suite/merge/merge.result

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,3 +4031,31 @@ UPDATE v1 SET a=0;
40314031
DROP VIEW v1;
40324032
DROP TABLE t1;
40334033
# End of 11.1 tests
4034+
#
4035+
# MDEV-38474 Double free or corruption, ASAN heap-use-after-free in st_join_table::cleanup
4036+
#
4037+
# Test case 1, fails on 10.11+
4038+
CREATE TABLE t1 (a INT);
4039+
CREATE TABLE t2 (b INT);
4040+
CREATE TABLE t3 (c INT);
4041+
# Inserts are optional, fails with and without data
4042+
INSERT INTO t1 VALUES (1),(2);
4043+
INSERT INTO t2 VALUES (3),(4);
4044+
INSERT INTO t3 VALUES (5),(6);
4045+
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT b FROM t2 WHERE a IN ((SELECT c FROM t3 WHERE FALSE HAVING c < 0)));
4046+
id select_type table type possible_keys key key_len ref rows Extra
4047+
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
4048+
3 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
4049+
DROP TABLE t1, t2, t3;
4050+
# Test case 2, fails on 11.4 but not on 10.11
4051+
CREATE TABLE t1 (a INT);
4052+
CREATE TABLE t2 (b INT);
4053+
CREATE TABLE t3 (c INT);
4054+
CREATE TABLE t4 (d INT PRIMARY KEY);
4055+
SET SQL_SAFE_UPDATES=1;
4056+
UPDATE t1 STRAIGHT_JOIN t2 SET a = 89 WHERE 9 IN (SELECT c FROM t3 WHERE c IN (SELECT MAX(d) FROM t4));
4057+
ERROR HY000: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
4058+
DROP TABLE t1, t2, t3, t4;
4059+
#
4060+
# End of 11.4 tests
4061+
#

mysql-test/suite/merge/merge.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,3 +2965,37 @@ DROP VIEW v1;
29652965
DROP TABLE t1;
29662966

29672967
--echo # End of 11.1 tests
2968+
2969+
--echo #
2970+
--echo # MDEV-38474 Double free or corruption, ASAN heap-use-after-free in st_join_table::cleanup
2971+
--echo #
2972+
2973+
--echo # Test case 1, fails on 10.11+
2974+
CREATE TABLE t1 (a INT);
2975+
CREATE TABLE t2 (b INT);
2976+
CREATE TABLE t3 (c INT);
2977+
2978+
--echo # Inserts are optional, fails with and without data
2979+
INSERT INTO t1 VALUES (1),(2);
2980+
INSERT INTO t2 VALUES (3),(4);
2981+
INSERT INTO t3 VALUES (5),(6);
2982+
2983+
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT b FROM t2 WHERE a IN ((SELECT c FROM t3 WHERE FALSE HAVING c < 0)));
2984+
2985+
DROP TABLE t1, t2, t3;
2986+
2987+
--echo # Test case 2, fails on 11.4 but not on 10.11
2988+
CREATE TABLE t1 (a INT);
2989+
CREATE TABLE t2 (b INT);
2990+
CREATE TABLE t3 (c INT);
2991+
CREATE TABLE t4 (d INT PRIMARY KEY);
2992+
2993+
SET SQL_SAFE_UPDATES=1;
2994+
--error ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE
2995+
UPDATE t1 STRAIGHT_JOIN t2 SET a = 89 WHERE 9 IN (SELECT c FROM t3 WHERE c IN (SELECT MAX(d) FROM t4));
2996+
2997+
DROP TABLE t1, t2, t3, t4;
2998+
2999+
--echo #
3000+
--echo # End of 11.4 tests
3001+
--echo #

sql/sql_union.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,8 +2688,6 @@ bool st_select_lex_unit::exec_recursive()
26882688

26892689
bool st_select_lex_unit::cleanup()
26902690
{
2691-
cleanup_stranded_units();
2692-
26932691
bool error= 0;
26942692
DBUG_ENTER("st_select_lex_unit::cleanup");
26952693

@@ -2778,6 +2776,7 @@ bool st_select_lex_unit::cleanup()
27782776
delete pushdown_unit;
27792777
pushdown_unit= nullptr;
27802778

2779+
cleanup_stranded_units();
27812780
DBUG_RETURN(error);
27822781
}
27832782

0 commit comments

Comments
 (0)