Skip to content

Commit baacdf1

Browse files
Sergey GlukhovSergey Glukhov
authored andcommitted
Bug#49501 Inefficient information_schema check (system collation)
added check_length optimization for I_S_NAME comparison
1 parent 22cff39 commit baacdf1

File tree

9 files changed

+49
-41
lines changed

9 files changed

+49
-41
lines changed

sql/event_data_objects.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ Event_job_data::execute(THD *thd, bool drop)
14011401
#endif
14021402

14031403
if (check_access(thd, EVENT_ACL, dbname.str,
1404-
0, 0, 0, is_schema_db(dbname.str)))
1404+
0, 0, 0, is_schema_db(dbname.str, dbname.length)))
14051405
{
14061406
/*
14071407
This aspect of behavior is defined in the worklog,

sql/events.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ Events::create_event(THD *thd, Event_parse_data *parse_data,
415415
DBUG_ASSERT(parse_data->expression || parse_data->execute_at);
416416

417417
if (check_access(thd, EVENT_ACL, parse_data->dbname.str, 0, 0, 0,
418-
is_schema_db(parse_data->dbname.str)))
418+
is_schema_db(parse_data->dbname.str,
419+
parse_data->dbname.length)))
419420
DBUG_RETURN(TRUE);
420421

421422
if (check_db_dir_existence(parse_data->dbname.str))
@@ -526,7 +527,8 @@ Events::update_event(THD *thd, Event_parse_data *parse_data,
526527
DBUG_RETURN(TRUE);
527528

528529
if (check_access(thd, EVENT_ACL, parse_data->dbname.str, 0, 0, 0,
529-
is_schema_db(parse_data->dbname.str)))
530+
is_schema_db(parse_data->dbname.str,
531+
parse_data->dbname.length)))
530532
DBUG_RETURN(TRUE);
531533

532534
if (new_dbname) /* It's a rename */
@@ -548,7 +550,7 @@ Events::update_event(THD *thd, Event_parse_data *parse_data,
548550
access it.
549551
*/
550552
if (check_access(thd, EVENT_ACL, new_dbname->str, 0, 0, 0,
551-
is_schema_db(new_dbname->str)))
553+
is_schema_db(new_dbname->str, new_dbname->length)))
552554
DBUG_RETURN(TRUE);
553555

554556
/* Check that the target database exists */
@@ -653,7 +655,7 @@ Events::drop_event(THD *thd, LEX_STRING dbname, LEX_STRING name, bool if_exists)
653655
DBUG_RETURN(TRUE);
654656

655657
if (check_access(thd, EVENT_ACL, dbname.str, 0, 0, 0,
656-
is_schema_db(dbname.str)))
658+
is_schema_db(dbname.str, dbname.length)))
657659
DBUG_RETURN(TRUE);
658660

659661
/*
@@ -811,7 +813,7 @@ Events::show_create_event(THD *thd, LEX_STRING dbname, LEX_STRING name)
811813
DBUG_RETURN(TRUE);
812814

813815
if (check_access(thd, EVENT_ACL, dbname.str, 0, 0, 0,
814-
is_schema_db(dbname.str)))
816+
is_schema_db(dbname.str, dbname.length)))
815817
DBUG_RETURN(TRUE);
816818

817819
/*
@@ -869,7 +871,8 @@ Events::fill_schema_events(THD *thd, TABLE_LIST *tables, COND * /* cond */)
869871
if (thd->lex->sql_command == SQLCOM_SHOW_EVENTS)
870872
{
871873
DBUG_ASSERT(thd->lex->select_lex.db);
872-
if (!is_schema_db(thd->lex->select_lex.db) && // There is no events in I_S
874+
if (!is_schema_db(thd->lex->select_lex.db, // There is no events in I_S
875+
strlen(thd->lex->select_lex.db)) &&
873876
check_access(thd, EVENT_ACL, thd->lex->select_lex.db, 0, 0, 0, 0))
874877
DBUG_RETURN(1);
875878
db= thd->lex->select_lex.db;

sql/mysql_priv.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,8 +1415,12 @@ bool get_schema_tables_result(JOIN *join,
14151415
enum enum_schema_table_state executed_place);
14161416
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table);
14171417

1418-
#define is_schema_db(X) \
1419-
!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.str, (X))
1418+
inline bool is_schema_db(const char *name, size_t len)
1419+
{
1420+
return (INFORMATION_SCHEMA_NAME.length == len &&
1421+
!my_strcasecmp(system_charset_info,
1422+
INFORMATION_SCHEMA_NAME.str, name));
1423+
}
14201424

14211425
/* sql_prepare.cc */
14221426

sql/repl_failsafe.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ bool load_master_data(THD* thd)
905905
if (!rpl_filter->db_ok(db) ||
906906
!rpl_filter->db_ok_with_wild_table(db) ||
907907
!strcmp(db,"mysql") ||
908-
is_schema_db(db))
908+
is_schema_db(db, strlen(db)))
909909
{
910910
*cur_table_res = 0;
911911
continue;

sql/sql_db.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ int mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create_info,
618618
DBUG_ENTER("mysql_create_db");
619619

620620
/* do not create 'information_schema' db */
621-
if (!my_strcasecmp(system_charset_info, db, INFORMATION_SCHEMA_NAME.str))
621+
if (is_schema_db(db, strlen(db)))
622622
{
623623
my_error(ER_DB_CREATE_EXISTS, MYF(0), db);
624624
DBUG_RETURN(-1);
@@ -1557,8 +1557,7 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch)
15571557
}
15581558
}
15591559

1560-
if (my_strcasecmp(system_charset_info, new_db_name->str,
1561-
INFORMATION_SCHEMA_NAME.str) == 0)
1560+
if (is_schema_db(new_db_name->str, new_db_name->length))
15621561
{
15631562
/* Switch the current database to INFORMATION_SCHEMA. */
15641563

sql/sql_parse.cc

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
13051305
table_list.alias= table_list.table_name= conv_name.str;
13061306
packet= arg_end + 1;
13071307

1308-
if (!my_strcasecmp(system_charset_info, table_list.db,
1309-
INFORMATION_SCHEMA_NAME.str))
1308+
if (is_schema_db(table_list.db, table_list.db_length))
13101309
{
13111310
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
13121311
if (schema_table)
@@ -1368,7 +1367,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
13681367
break;
13691368
}
13701369
if (check_access(thd, CREATE_ACL, db.str , 0, 1, 0,
1371-
is_schema_db(db.str)))
1370+
is_schema_db(db.str, db.length)))
13721371
break;
13731372
general_log_print(thd, command, "%.*s", db.length, db.str);
13741373
bzero(&create_info, sizeof(create_info));
@@ -1387,7 +1386,8 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
13871386
my_error(ER_WRONG_DB_NAME, MYF(0), db.str ? db.str : "NULL");
13881387
break;
13891388
}
1390-
if (check_access(thd, DROP_ACL, db.str, 0, 1, 0, is_schema_db(db.str)))
1389+
if (check_access(thd, DROP_ACL, db.str, 0, 1, 0,
1390+
is_schema_db(db.str, db.length)))
13911391
break;
13921392
if (thd->locked_tables || thd->active_transaction())
13931393
{
@@ -2852,7 +2852,7 @@ mysql_execute_command(THD *thd)
28522852
&first_table->grant.privilege, 0, 0,
28532853
test(first_table->schema_table)) ||
28542854
check_access(thd,INSERT_ACL | CREATE_ACL,select_lex->db,&priv,0,0,
2855-
is_schema_db(select_lex->db))||
2855+
is_schema_db(select_lex->db, strlen(select_lex->db)))||
28562856
check_merge_table_access(thd, first_table->db,
28572857
(TABLE_LIST *)
28582858
create_info.merge_list.first))
@@ -3590,7 +3590,7 @@ mysql_execute_command(THD *thd)
35903590
}
35913591
#endif
35923592
if (check_access(thd,CREATE_ACL,lex->name.str, 0, 1, 0,
3593-
is_schema_db(lex->name.str)))
3593+
is_schema_db(lex->name.str, lex->name.length)))
35943594
break;
35953595
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
35963596
lex->name.str), &create_info, 0);
@@ -3625,7 +3625,7 @@ mysql_execute_command(THD *thd)
36253625
}
36263626
#endif
36273627
if (check_access(thd,DROP_ACL,lex->name.str,0,1,0,
3628-
is_schema_db(lex->name.str)))
3628+
is_schema_db(lex->name.str, lex->name.length)))
36293629
break;
36303630
if (thd->locked_tables || thd->active_transaction())
36313631
{
@@ -3659,9 +3659,12 @@ mysql_execute_command(THD *thd)
36593659
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
36603660
break;
36613661
}
3662-
if (check_access(thd, ALTER_ACL, db->str, 0, 1, 0, is_schema_db(db->str)) ||
3663-
check_access(thd, DROP_ACL, db->str, 0, 1, 0, is_schema_db(db->str)) ||
3664-
check_access(thd, CREATE_ACL, db->str, 0, 1, 0, is_schema_db(db->str)))
3662+
if (check_access(thd, ALTER_ACL, db->str, 0, 1, 0,
3663+
is_schema_db(db->str, db->length)) ||
3664+
check_access(thd, DROP_ACL, db->str, 0, 1, 0,
3665+
is_schema_db(db->str, db->length)) ||
3666+
check_access(thd, CREATE_ACL, db->str, 0, 1, 0,
3667+
is_schema_db(db->str, db->length)))
36653668
{
36663669
res= 1;
36673670
break;
@@ -3704,7 +3707,8 @@ mysql_execute_command(THD *thd)
37043707
break;
37053708
}
37063709
#endif
3707-
if (check_access(thd, ALTER_ACL, db->str, 0, 1, 0, is_schema_db(db->str)))
3710+
if (check_access(thd, ALTER_ACL, db->str, 0, 1, 0,
3711+
is_schema_db(db->str, db->length)))
37083712
break;
37093713
if (thd->locked_tables || thd->active_transaction())
37103714
{
@@ -3860,7 +3864,8 @@ mysql_execute_command(THD *thd)
38603864
first_table ? &first_table->grant.privilege : 0,
38613865
first_table ? 0 : 1, 0,
38623866
first_table ? (bool) first_table->schema_table :
3863-
select_lex->db ? is_schema_db(select_lex->db) : 0))
3867+
select_lex->db ?
3868+
is_schema_db(select_lex->db, strlen(select_lex->db)) : 0))
38643869
goto error;
38653870

38663871
if (thd->security_ctx->user) // If not replication
@@ -4203,7 +4208,8 @@ mysql_execute_command(THD *thd)
42034208
}
42044209

42054210
if (check_access(thd, CREATE_PROC_ACL, lex->sphead->m_db.str, 0, 0, 0,
4206-
is_schema_db(lex->sphead->m_db.str)))
4211+
is_schema_db(lex->sphead->m_db.str,
4212+
lex->sphead->m_db.length)))
42074213
goto create_sp_error;
42084214

42094215
if (end_active_trans(thd))
@@ -4858,7 +4864,8 @@ mysql_execute_command(THD *thd)
48584864
res= mysql_xa_recover(thd);
48594865
break;
48604866
case SQLCOM_ALTER_TABLESPACE:
4861-
if (check_access(thd, ALTER_ACL, thd->db, 0, 1, 0, thd->db ? is_schema_db(thd->db) : 0))
4867+
if (check_access(thd, ALTER_ACL, thd->db, 0, 1, 0,
4868+
thd->db ? is_schema_db(thd->db, thd->db_length) : 0))
48624869
break;
48634870
if (!(res= mysql_alter_tablespace(thd, lex->alter_tablespace_info)))
48644871
my_ok(thd);
@@ -5297,7 +5304,7 @@ static bool check_show_access(THD *thd, TABLE_LIST *table)
52975304

52985305
if (check_access(thd, SELECT_ACL, dst_db_name,
52995306
&thd->col_access, FALSE, FALSE,
5300-
is_schema_db(dst_db_name)))
5307+
is_schema_db(dst_db_name, strlen(dst_db_name))))
53015308
return TRUE;
53025309

53035310
if (!thd->col_access && check_grant_db(thd, dst_db_name))
@@ -6262,8 +6269,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
62626269
ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
62636270
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
62646271
ptr->derived= table->sel;
6265-
if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
6266-
INFORMATION_SCHEMA_NAME.str))
6272+
if (!ptr->derived && is_schema_db(ptr->db, ptr->db_length))
62676273
{
62686274
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
62696275
if (!schema_table ||

sql/sql_show.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,7 @@ bool mysqld_show_create_db(THD *thd, char *dbname,
826826
DBUG_RETURN(TRUE);
827827
}
828828
#endif
829-
if (!my_strcasecmp(system_charset_info, dbname,
830-
INFORMATION_SCHEMA_NAME.str))
829+
if (is_schema_db(dbname, strlen(dbname)))
831830
{
832831
dbname= INFORMATION_SCHEMA_NAME.str;
833832
create.default_table_charset= system_charset_info;
@@ -2780,8 +2779,8 @@ int make_db_list(THD *thd, List<LEX_STRING> *files,
27802779
*/
27812780
if (lookup_field_vals->db_value.str)
27822781
{
2783-
if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.str,
2784-
lookup_field_vals->db_value.str))
2782+
if (is_schema_db(lookup_field_vals->db_value.str,
2783+
lookup_field_vals->db_value.length))
27852784
{
27862785
*with_i_schema= 1;
27872786
if (files->push_back(i_s_name_copy))
@@ -5228,7 +5227,7 @@ copy_event_to_schema_table(THD *thd, TABLE *sch_table, TABLE *event_table)
52285227
*/
52295228
if (thd->lex->sql_command != SQLCOM_SHOW_EVENTS &&
52305229
check_access(thd, EVENT_ACL, et.dbname.str, 0, 0, 1,
5231-
is_schema_db(et.dbname.str)))
5230+
is_schema_db(et.dbname.str, et.dbname.length)))
52325231
DBUG_RETURN(0);
52335232

52345233
/* ->field[0] is EVENT_CATALOG and is by default NULL */

sql/sql_view.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ bool create_view_precheck(THD *thd, TABLE_LIST *tables, TABLE_LIST *view,
268268
table (i.e. user will not get some privileges by view creation)
269269
*/
270270
if ((check_access(thd, CREATE_VIEW_ACL, view->db, &view->grant.privilege,
271-
0, 0, is_schema_db(view->db)) ||
271+
0, 0, is_schema_db(view->db, view->db_length)) ||
272272
check_grant(thd, CREATE_VIEW_ACL, view, 0, 1, 0)) ||
273273
(mode != VIEW_CREATE_NEW &&
274274
(check_access(thd, DROP_ACL, view->db, &view->grant.privilege,
275-
0, 0, is_schema_db(view->db)) ||
275+
0, 0, is_schema_db(view->db, view->db_length)) ||
276276
check_grant(thd, DROP_ACL, view, 0, 1, 0))))
277277
goto err;
278278

sql/table.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ TABLE_CATEGORY get_table_category(const LEX_STRING *db, const LEX_STRING *name)
212212
DBUG_ASSERT(db != NULL);
213213
DBUG_ASSERT(name != NULL);
214214

215-
if ((db->length == INFORMATION_SCHEMA_NAME.length) &&
216-
(my_strcasecmp(system_charset_info,
217-
INFORMATION_SCHEMA_NAME.str,
218-
db->str) == 0))
215+
if (is_schema_db(db->str, db->length))
219216
{
220217
return TABLE_CATEGORY_INFORMATION;
221218
}

0 commit comments

Comments
 (0)