Skip to content

Commit 6e87c47

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.subject AS fld FROM v1 AS tbl GROUP BY fld HAVING 0 AND fld != 1'; Here v1 is a view on top of a table having a single longtext column. The column "subject" in v1 is defined as an expression using "ifnull" function. 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 "subject". 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 6e87c47

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

mysql-test/main/having.result

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,22 @@ DROP TABLE t1,t2;
10011001
#
10021002
# End of 10.5 tests
10031003
#
1004+
#
1005+
# MDEV-35815: Crash in ASAN build due to accessing use-after-poison memory error
1006+
#
1007+
SET @save_optimizer_trace=@@optimizer_trace;
1008+
SET optimizer_trace= 'enabled=on';
1009+
CREATE TABLE t1 (subject LONGTEXT);
1010+
INSERT INTO t1 VALUES ('a'), ('b');
1011+
CREATE VIEW v1 AS (SELECT ifnull(json_value(subject,'$.subject'),'') AS subject FROM t1);
1012+
PREPARE stmt FROM 'SELECT tbl.subject AS fld FROM v1 AS tbl GROUP BY fld HAVING 0 AND fld != 1';
1013+
EXECUTE stmt;
1014+
fld
1015+
EXECUTE stmt;
1016+
fld
1017+
DROP TABLE t1;
1018+
DROP VIEW v1;
1019+
SET optimizer_trace= @save_optimizer_trace;
1020+
#
1021+
# End of 10.11 tests
1022+
#

mysql-test/main/having.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,25 @@ DROP TABLE t1,t2;
10561056
--echo #
10571057
--echo # End of 10.5 tests
10581058
--echo #
1059+
1060+
--echo #
1061+
--echo # MDEV-35815: Crash in ASAN build due to accessing use-after-poison memory error
1062+
--echo #
1063+
SET @save_optimizer_trace=@@optimizer_trace;
1064+
SET optimizer_trace= 'enabled=on';
1065+
1066+
CREATE TABLE t1 (subject LONGTEXT);
1067+
INSERT INTO t1 VALUES ('a'), ('b');
1068+
CREATE VIEW v1 AS (SELECT ifnull(json_value(subject,'$.subject'),'') AS subject FROM t1);
1069+
1070+
PREPARE stmt FROM 'SELECT tbl.subject AS fld FROM v1 AS tbl GROUP BY fld HAVING 0 AND fld != 1';
1071+
1072+
EXECUTE stmt;
1073+
EXECUTE stmt;
1074+
1075+
DROP TABLE t1;
1076+
DROP VIEW v1;
1077+
SET optimizer_trace= @save_optimizer_trace;
1078+
--echo #
1079+
--echo # End of 10.11 tests
1080+
--echo #

sql/sql_select.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28086,6 +28086,13 @@ 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 (pos->name.length > 0)
28090+
{
28091+
Query_arena_stmt on_stmt_arena(thd);
28092+
if (on_stmt_arena.arena_replaced() &&
28093+
!(thd->mem_root->flags & ROOT_FLAG_READ_ONLY))
28094+
pos->name.str= strdup_root(thd->mem_root, pos->name.str);
28095+
}
2808928096
LEX_CSTRING real_name= pos->name;
2809028097
pos= real_pos;
2809128098
pos->name= real_name;

0 commit comments

Comments
 (0)