diff --git a/sql/sql_base.cc b/sql/sql_base.cc index bcab54ac1ec..5c03ba3d42d 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -7862,11 +7862,15 @@ bool setup_tables(THD *thd, Name_resolution_context *context, DBUG_RETURN(1); } tablenr++; - } - if (tablenr > MAX_TABLES) - { - my_error(ER_TOO_MANY_TABLES,MYF(0), static_cast(MAX_TABLES)); - DBUG_RETURN(1); + /* + Test MAX_TABLES overflow here inside the loop as setup_table_map() + called in each iteration is sensitive for this + */ + if (tablenr > MAX_TABLES) + { + my_error(ER_TOO_MANY_TABLES, MYF(0), static_cast(MAX_TABLES)); + DBUG_RETURN(1); + } } if (select_insert && !is_insert_tables_num_set) { diff --git a/sql/sql_base.h b/sql/sql_base.h index 894c8213e66..90c47e69d94 100644 --- a/sql/sql_base.h +++ b/sql/sql_base.h @@ -353,6 +353,7 @@ inline void setup_table_map(TABLE *table, TABLE_LIST *table_list, uint tablenr) table->maybe_null= embedding->outer_join; embedding= embedding->embedding; } + DBUG_ASSERT(tablenr <= MAX_TABLES); table->tablenr= tablenr; table->map= (table_map) 1 << tablenr; table->force_index= table_list->force_index;