Skip to content

Commit 90629aa

Browse files
spetruniaAlexey Botchkov
authored andcommitted
MDEV-25346: JSON_TABLE: Server crashes in Item_field::fix_outer_field ...
mysql_derived_prepare() sets Name_resolution_context::outer_context=NULL for the WHERE clause's context. Do the same for all ON expressions, too.
1 parent bd1d6ee commit 90629aa

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

mysql-test/suite/json/r/json_table.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,17 @@ RIGHT JOIN JSON_TABLE('[]', '$' COLUMNS(o3 FOR ORDINALITY)) AS jt3
704704
ON(1)
705705
WHERE 0;
706706
ERROR 42S22: Unknown column 'jt1.a' in 'JSON_TABLE argument'
707+
#
708+
# MDEV-25346: JSON_TABLE: Server crashes in Item_field::fix_outer_field upon subquery with unknown column
709+
#
710+
CREATE TABLE t1 (a INT);
711+
CREATE TABLE t2 (b INT);
712+
SELECT * FROM ( SELECT * FROM t1 JOIN t2 ON (b IN(SELECT x FROM (SELECT 1 AS c) AS sq1))) AS sq2;
713+
ERROR 42S22: Unknown column 'x' in 'field list'
714+
DROP TABLE t1, t2;
715+
#
716+
# Another testcase
717+
#
707718
create table t1 (item_name varchar(32), item_props varchar(1024));
708719
insert into t1 values ('Jeans', '{"color": ["green", "brown"], "price": 50}');
709720
insert into t1 values ('Shirt', '{"color": ["blue", "white"], "price": 20}');

mysql-test/suite/json/t/json_table.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,20 @@ JSON_TABLE('[]', '$' COLUMNS(a TEXT PATH '$[*]')) AS jt1
601601
ON(1)
602602
WHERE 0;
603603

604+
--echo #
605+
--echo # MDEV-25346: JSON_TABLE: Server crashes in Item_field::fix_outer_field upon subquery with unknown column
606+
--echo #
607+
CREATE TABLE t1 (a INT);
608+
CREATE TABLE t2 (b INT);
609+
610+
--error ER_BAD_FIELD_ERROR
611+
SELECT * FROM ( SELECT * FROM t1 JOIN t2 ON (b IN(SELECT x FROM (SELECT 1 AS c) AS sq1))) AS sq2;
612+
613+
DROP TABLE t1, t2;
614+
615+
--echo #
616+
--echo # Another testcase
617+
--echo #
604618
create table t1 (item_name varchar(32), item_props varchar(1024));
605619
insert into t1 values ('Jeans', '{"color": ["green", "brown"], "price": 50}');
606620
insert into t1 values ('Shirt', '{"color": ["blue", "white"], "price": 20}');

sql/sql_derived.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
#include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */
26+
#include <functional>
2627
#include "sql_priv.h"
2728
#include "unireg.h"
2829
#include "sql_derived.h"
@@ -760,7 +761,24 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
760761
/* prevent name resolving out of derived table */
761762
for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
762763
{
764+
// Prevent it for the WHERE clause
763765
sl->context.outer_context= 0;
766+
767+
// And for ON clauses, if there are any
768+
std::function<void(List<TABLE_LIST>&)> reset_context=
769+
[&](List<TABLE_LIST> &join_list)
770+
{
771+
List_iterator<TABLE_LIST> li(join_list);
772+
while (TABLE_LIST *table= li++)
773+
{
774+
if (table->on_context)
775+
table->on_context->outer_context= NULL;
776+
if (table->nested_join)
777+
reset_context(table->nested_join->join_list);
778+
}
779+
};
780+
reset_context(*sl->join_list);
781+
764782
if (!derived->is_with_table_recursive_reference() ||
765783
(!derived->with->with_anchor &&
766784
!derived->with->is_with_prepared_anchor()))

0 commit comments

Comments
 (0)