Skip to content

Commit 796d54d

Browse files
committed
MDEV-16957: Server crashes in Field_iterator_natural_join::next upon 2nd execution of SP
The problem was that join_columns creation was not finished due to error of notfound column in USING, but next execution tried to use join_columns lists. Solution is cleanup the lists on error. It can eat memory in statement MEM_ROOT but it is an error and error will be fixed or statement/procedure removed/altered.
1 parent 42f09ad commit 796d54d

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed

mysql-test/r/join.result

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,11 +1514,13 @@ ERROR 42S22: Unknown column 'f' in 'from clause'
15141514
DROP TABLE t;
15151515
CREATE TABLE t (f INT);
15161516
CALL p;
1517-
ERROR 42S22: Unknown column 'f' in 'from clause'
1517+
f
15181518
DROP TABLE t;
15191519
CREATE TABLE t (i INT);
15201520
CALL p;
1521-
ERROR 42S22: Unknown column 'f' in 'from clause'
1521+
ERROR 42S22: Unknown column 't1.f' in 'field list'
1522+
CALL p;
1523+
ERROR 42S22: Unknown column 't1.f' in 'field list'
15221524
DROP PROCEDURE p;
15231525
DROP TABLE t;
15241526
CREATE TABLE t1 (a INT, b INT);

mysql-test/r/sp.result

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8055,4 +8055,21 @@ SET S.CLOSE_YN = ''
80558055
where 1=1;
80568056
drop function if exists f1;
80578057
drop table t1,t2;
8058+
#
8059+
# MDEV-16957: Server crashes in Field_iterator_natural_join::next
8060+
# upon 2nd execution of SP
8061+
#
8062+
CREATE TABLE t1 (a INT, b VARCHAR(32));
8063+
CREATE PROCEDURE sp() SELECT * FROM t1 AS t1x JOIN t1 AS t1y USING (c);
8064+
CALL sp;
8065+
ERROR 42S22: Unknown column 'c' in 'from clause'
8066+
CALL sp;
8067+
ERROR 42S22: Unknown column 'c' in 'from clause'
8068+
CALL sp;
8069+
ERROR 42S22: Unknown column 'c' in 'from clause'
8070+
alter table t1 add column c int;
8071+
CALL sp;
8072+
c a b a b
8073+
DROP PROCEDURE sp;
8074+
DROP TABLE t1;
80588075
# End of 5.5 test

mysql-test/t/join.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,13 @@ CREATE TABLE t (f INT);
11851185
#
11861186
# The following shouldn't fail as the table is now matching the using
11871187
#
1188-
--error ER_BAD_FIELD_ERROR
11891188
CALL p;
11901189
DROP TABLE t;
11911190
CREATE TABLE t (i INT);
11921191
--error ER_BAD_FIELD_ERROR
11931192
CALL p;
1193+
--error ER_BAD_FIELD_ERROR
1194+
CALL p;
11941195
DROP PROCEDURE p;
11951196
DROP TABLE t;
11961197

mysql-test/t/sp.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9353,4 +9353,25 @@ where 1=1;
93539353
drop function if exists f1;
93549354
drop table t1,t2;
93559355

9356+
--echo #
9357+
--echo # MDEV-16957: Server crashes in Field_iterator_natural_join::next
9358+
--echo # upon 2nd execution of SP
9359+
--echo #
9360+
9361+
CREATE TABLE t1 (a INT, b VARCHAR(32));
9362+
CREATE PROCEDURE sp() SELECT * FROM t1 AS t1x JOIN t1 AS t1y USING (c);
9363+
--error ER_BAD_FIELD_ERROR
9364+
CALL sp;
9365+
--error ER_BAD_FIELD_ERROR
9366+
CALL sp;
9367+
--error ER_BAD_FIELD_ERROR
9368+
CALL sp;
9369+
alter table t1 add column c int;
9370+
CALL sp;
9371+
9372+
# Cleanup
9373+
DROP PROCEDURE sp;
9374+
DROP TABLE t1;
9375+
9376+
93569377
--echo # End of 5.5 test

sql/sql_base.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7764,10 +7764,22 @@ store_natural_using_join_columns(THD *thd, TABLE_LIST *natural_using_join,
77647764

77657765
result= FALSE;
77667766

7767-
err:
77687767
if (arena)
77697768
thd->restore_active_arena(arena, &backup);
77707769
DBUG_RETURN(result);
7770+
7771+
err:
7772+
/*
7773+
Actually we failed to build join columns list, so we have to
7774+
clear it to avoid problems with half-build join on next run.
7775+
The list was created in mark_common_columns().
7776+
*/
7777+
table_ref_1->remove_join_columns();
7778+
table_ref_2->remove_join_columns();
7779+
7780+
if (arena)
7781+
thd->restore_active_arena(arena, &backup);
7782+
DBUG_RETURN(TRUE);
77717783
}
77727784

77737785

sql/table.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,16 @@ struct TABLE_LIST
21842184
}
21852185
void set_lock_type(THD* thd, enum thr_lock_type lock);
21862186

2187+
void remove_join_columns()
2188+
{
2189+
if (join_columns)
2190+
{
2191+
join_columns->empty();
2192+
join_columns= NULL;
2193+
is_join_columns_complete= FALSE;
2194+
}
2195+
}
2196+
21872197
private:
21882198
bool prep_check_option(THD *thd, uint8 check_opt_type);
21892199
bool prep_where(THD *thd, Item **conds, bool no_where_clause);

0 commit comments

Comments
 (0)