Skip to content

Commit 73f415c

Browse files
committed
MDEV-24935: Server crashes in Field_iterator_natural_join::next or Field_iterator_table_ref::set_field_iterator upon 2nd execution of SP
Calling a stored routine that executes a join on three or more tables and referencing not-existent column name in the USING clause resulted in a crash on its second invocation. Server crash taken place by the reason of dereferencing null pointer in condition of DBUG_ASSERT inside the method Field_iterator_natural_join::next() There the data member cur_column_ref->table_field->field has the nullptr value that was reset at the end of first execution of a stored routine when the standalone procedure cleanup_items() called by the method sp_head::execute. Later this data member is not re-initialized and never referenced in any place except the DBUG_ASSERT on second and later invocations of the stored routine. To fix the issue, the assert's condition should be augmented by a condition '|| !cur_column_ref->table_field' before dereferencing cur_column_ref->table_field. Such extra checking is aligned with conditions used by DBUG_ASSERT macros used by implementation of the class Field_iterator_table_ref that aggregated the class Field_iterator_natural_join.
1 parent d261fa5 commit 73f415c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

mysql-test/main/sp-bugs.result

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,30 @@ ERROR HY000: Unknown thread id: 0
363363
#
364364
# End of 10.4 tests
365365
#
366+
#
367+
# MDEV-24935: Server crashes in Field_iterator_natural_join::next or Field_iterator_table_ref::set_field_iterator upon 2nd execution of SP
368+
#
369+
CREATE TABLE t1 (a INT);
370+
CREATE TABLE t2 (b INT, c INT);
371+
CREATE TABLE t3 (d INT);
372+
CREATE PROCEDURE sp() SELECT * FROM t1 JOIN t2 JOIN t3 USING (x);
373+
CALL sp;
374+
ERROR 42S22: Unknown column 'x' in 'from clause'
375+
CALL sp;
376+
ERROR 42S22: Unknown column 'x' in 'from clause'
377+
# Clean up
378+
DROP PROCEDURE sp;
379+
DROP TABLE t1, t2, t3;
380+
CREATE TABLE t1 (c1 INT,c2 INT);
381+
CREATE TABLE t2 (c INT,c2 INT);
382+
CREATE PROCEDURE p2 (OUT i INT,OUT o INT) READS SQL DATA DELETE a2,a3 FROM t1 AS a1 JOIN t2 AS a2 NATURAL JOIN t2 AS a3;
383+
CALL p2 (@c,@a);
384+
ERROR 23000: Column 'c2' in from clause is ambiguous
385+
CALL p2 (@a,@c);
386+
ERROR 23000: Column 'c2' in from clause is ambiguous
387+
# Clean up
388+
DROP PROCEDURE p2;
389+
DROP TABLE t1, t2;
390+
#
391+
# End of 10.5 tests
392+
#

mysql-test/main/sp-bugs.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,34 @@ KILL (('x' IN ( SELECT 1)) MOD 44);
386386
--echo #
387387
--echo # End of 10.4 tests
388388
--echo #
389+
390+
--echo #
391+
--echo # MDEV-24935: Server crashes in Field_iterator_natural_join::next or Field_iterator_table_ref::set_field_iterator upon 2nd execution of SP
392+
--echo #
393+
CREATE TABLE t1 (a INT);
394+
CREATE TABLE t2 (b INT, c INT);
395+
CREATE TABLE t3 (d INT);
396+
CREATE PROCEDURE sp() SELECT * FROM t1 JOIN t2 JOIN t3 USING (x);
397+
--error ER_BAD_FIELD_ERROR
398+
CALL sp;
399+
--error ER_BAD_FIELD_ERROR
400+
CALL sp;
401+
--echo # Clean up
402+
DROP PROCEDURE sp;
403+
DROP TABLE t1, t2, t3;
404+
405+
CREATE TABLE t1 (c1 INT,c2 INT);
406+
CREATE TABLE t2 (c INT,c2 INT);
407+
CREATE PROCEDURE p2 (OUT i INT,OUT o INT) READS SQL DATA DELETE a2,a3 FROM t1 AS a1 JOIN t2 AS a2 NATURAL JOIN t2 AS a3;
408+
409+
--error ER_NON_UNIQ_ERROR
410+
CALL p2 (@c,@a);
411+
--error ER_NON_UNIQ_ERROR
412+
CALL p2 (@a,@c);
413+
--echo # Clean up
414+
DROP PROCEDURE p2;
415+
DROP TABLE t1, t2;
416+
417+
--echo #
418+
--echo # End of 10.5 tests
419+
--echo #

sql/table.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6948,6 +6948,7 @@ void Field_iterator_natural_join::next()
69486948
{
69496949
cur_column_ref= column_ref_it++;
69506950
DBUG_ASSERT(!cur_column_ref || ! cur_column_ref->table_field ||
6951+
!cur_column_ref->table_field->field ||
69516952
cur_column_ref->table_ref->table ==
69526953
cur_column_ref->table_field->field->table);
69536954
}

0 commit comments

Comments
 (0)