Skip to content

Commit 6f36639

Browse files
MDEV-35815: use-after-poison_in_get_hash_symbol
When a PREPARED statment is executed twice, it is crashing during the second execution. For the following query: - PREPARE stmt FROM 'SELECT tbl.x509_subject AS fld FROM mysql.user AS tbl GROUP BY fld HAVING 0 AND fld != 1'; During the first execution: The Item "fld" in the HAVING clause, is an Item_ref instance with name "fld", where it has a ref to another Item_ref instance with the same name "fld" which in turn has a reference to another Item with name "x509_subjext". In the join prepare stage, while in the call to setup_copy_fields() from make_aggr_tables_info(), the name field of the last Item instance is replaced with the name in second Item_ref instance. However, after the first execution is done, the meory for the second Item_ref is freed. Now, during the second execution of the same statement, when trying to access name field Item_ref instance, it was getting crashed, because, that memory was already freed earlier. The fix is to allocate a new memory in the stmt_arena for the second Item_ref instance's name->str field.
1 parent 21395bb commit 6f36639

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

mysql-test/main/having.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,15 @@ DROP TABLE t1,t2;
10011001
#
10021002
# End of 10.5 tests
10031003
#
1004+
#
1005+
# Crash in ASAN build due to accessing use-after-poison memory error
1006+
#
1007+
SET optimizer_trace= 'enabled=on';
1008+
PREPARE stmt FROM 'SELECT tbl.x509_subject AS fld FROM mysql.user AS tbl GROUP BY fld HAVING 0 AND fld != 1';
1009+
EXECUTE stmt;
1010+
fld
1011+
EXECUTE stmt;
1012+
fld
1013+
#
1014+
# End of 10.11 tests
1015+
#

mysql-test/main/having.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,18 @@ DROP TABLE t1,t2;
10561056
--echo #
10571057
--echo # End of 10.5 tests
10581058
--echo #
1059+
1060+
--echo #
1061+
--echo # Crash in ASAN build due to accessing use-after-poison memory error
1062+
--echo #
1063+
1064+
SET optimizer_trace= 'enabled=on';
1065+
1066+
PREPARE stmt FROM 'SELECT tbl.x509_subject AS fld FROM mysql.user AS tbl GROUP BY fld HAVING 0 AND fld != 1';
1067+
1068+
EXECUTE stmt;
1069+
EXECUTE stmt;
1070+
1071+
--echo #
1072+
--echo # End of 10.11 tests
1073+
--echo #

sql/sql_select.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28086,6 +28086,10 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
2808628086
real_pos->type() == Item::COND_ITEM) &&
2808728087
!real_pos->with_sum_func())
2808828088
{ // Save for send fields
28089+
if (!thd->stmt_arena->is_conventional() &&
28090+
thd->mem_root != thd->stmt_arena->mem_root &&
28091+
!(thd->stmt_arena->mem_root->flags & ROOT_FLAG_READ_ONLY))
28092+
pos->name.str= strdup_root(thd->stmt_arena->mem_root, pos->name.str);
2808928093
LEX_CSTRING real_name= pos->name;
2809028094
pos= real_pos;
2809128095
pos->name= real_name;

0 commit comments

Comments
 (0)