Skip to content

Commit 6ffb219

Browse files
bsrikanth-mariadbvuvova
authored andcommitted
MDEV-32758: TRIM uses memory after freed
Item_func_trim::trimmed_value() prepares Item_func_trim::tmp_value to be the return value of Item_func_trim::val_str(). Before the fix, tmp_value would have pointer to the trimmed string, but didn't own it. This meant that second use of TRIM function could get to point to temporary buffer, then free the buffer and invalidate the return value of the first use of TRIM(). Avoid this by copying the return value into Item_func_trim::tmp_value.
1 parent ce20b2f commit 6ffb219

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

mysql-test/main/func_str.result

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,4 +5421,13 @@ x
54215421
NULL
54225422
Warnings:
54235423
Warning 1301 Result of hex() was larger than max_allowed_packet (16777216) - truncated
5424+
#
5425+
# MDEV-32758: TRIM uses memory after freed
5426+
#
5427+
CREATE TABLE t0 (i DOUBLE);
5428+
INSERT INTO t0 VALUES (1);
5429+
SELECT LOCATE(a , 'data1-data2', a) AS c FROM (SELECT TRIM(i) AS a FROM t0 )dt;
5430+
c
5431+
5
5432+
DROP TABLE t0;
54245433
# End of 10.6 tests

mysql-test/main/func_str.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,4 +2455,13 @@ DROP TABLE t1;
24552455
--echo #
24562456
select hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex(hex('\\'))))))))))))))))))))))))))))))))))))))))))))) as x;
24572457

2458+
--echo #
2459+
--echo # MDEV-32758: TRIM uses memory after freed
2460+
--echo #
2461+
CREATE TABLE t0 (i DOUBLE);
2462+
INSERT INTO t0 VALUES (1);
2463+
SELECT LOCATE(a , 'data1-data2', a) AS c FROM (SELECT TRIM(i) AS a FROM t0 )dt;
2464+
2465+
DROP TABLE t0;
2466+
24582467
--echo # End of 10.6 tests

sql/item_strfunc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,11 @@ class Item_func_trim :public Item_str_func
763763
if (length == 0)
764764
return make_empty_result(&tmp_value);
765765

766-
tmp_value.set(*res, offset, length);
766+
if (tmp_value.copy(res->ptr() + offset, length, res->charset()))
767+
{
768+
my_error(ER_OUTOFMEMORY, length);
769+
return NULL;
770+
}
767771
/*
768772
Make sure to return correct charset and collation:
769773
TRIM(0x000000 FROM _ucs2 0x0061)

0 commit comments

Comments
 (0)