Skip to content

Commit 7c3df72

Browse files
author
Alexander Barkov
committed
MDEV-13298 Change sp_head::m_chistics from a pointer to a structure
1 parent 31b3511 commit 7c3df72

14 files changed

+110
-76
lines changed

sql/event_data_objects.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,13 @@ Event_job_data::execute(THD *thd, bool drop)
14261426
sphead->m_flags|= sp_head::LOG_SLOW_STATEMENTS;
14271427
sphead->m_flags|= sp_head::LOG_GENERAL_LOG;
14281428

1429-
sphead->set_info(0, 0, &thd->lex->sp_chistics, sql_mode);
1429+
/*
1430+
construct_sp_sql() + parse_sql() set suid to SP_IS_NOT_SUID,
1431+
because we have the security context already set to the event
1432+
definer here. See more comments in construct_sp_sql().
1433+
*/
1434+
DBUG_ASSERT(sphead->suid() == SP_IS_NOT_SUID);
1435+
sphead->m_sql_mode= sql_mode;
14301436
sphead->set_creation_ctx(creation_ctx);
14311437
sphead->optimize();
14321438

sql/field.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ class Field: public Value_source
818818
{ return store(ls->str, ls->length, cs); }
819819
int store(const LEX_CSTRING *ls, CHARSET_INFO *cs)
820820
{ return store(ls->str, ls->length, cs); }
821+
int store(const LEX_CSTRING &ls, CHARSET_INFO *cs)
822+
{ return store(ls.str, ls.length, cs); }
821823
virtual double val_real(void)=0;
822824
virtual longlong val_int(void)=0;
823825
virtual bool val_bool(void)= 0;

sql/item_func.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6316,7 +6316,7 @@ Item_func_sp::init_result_field(THD *thd)
63166316

63176317
bool Item_func_sp::is_expensive()
63186318
{
6319-
return !m_sp->m_chistics->detistic ||
6319+
return !m_sp->detistic() ||
63206320
current_thd->locked_tables_mode < LTM_LOCK_TABLES;
63216321
}
63226322

@@ -6390,8 +6390,8 @@ Item_func_sp::execute_impl(THD *thd)
63906390
Sub_statement_state statement_state;
63916391
Security_context *save_security_ctx= thd->security_ctx;
63926392
enum enum_sp_data_access access=
6393-
(m_sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ?
6394-
SP_DEFAULT_ACCESS_MAPPING : m_sp->m_chistics->daccess;
6393+
(m_sp->daccess() == SP_DEFAULT_ACCESS) ?
6394+
SP_DEFAULT_ACCESS_MAPPING : m_sp->daccess();
63956395

63966396
DBUG_ENTER("Item_func_sp::execute_impl");
63976397

@@ -6408,7 +6408,7 @@ Item_func_sp::execute_impl(THD *thd)
64086408
statement-based replication (SBR) is active.
64096409
*/
64106410

6411-
if (!m_sp->m_chistics->detistic && !trust_function_creators &&
6411+
if (!m_sp->detistic() && !trust_function_creators &&
64126412
(access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) &&
64136413
(mysql_bin_log.is_open() &&
64146414
thd->variables.binlog_format == BINLOG_FORMAT_STMT))
@@ -6590,7 +6590,7 @@ Item_func_sp::fix_fields(THD *thd, Item **ref)
65906590
#endif /* ! NO_EMBEDDED_ACCESS_CHECKS */
65916591
}
65926592

6593-
if (!m_sp->m_chistics->detistic)
6593+
if (!m_sp->detistic())
65946594
{
65956595
used_tables_cache |= RAND_TABLE_BIT;
65966596
const_item_cache= FALSE;
@@ -6604,7 +6604,7 @@ void Item_func_sp::update_used_tables()
66046604
{
66056605
Item_func::update_used_tables();
66066606

6607-
if (!m_sp->m_chistics->detistic)
6607+
if (!m_sp->detistic())
66086608
{
66096609
used_tables_cache |= RAND_TABLE_BIT;
66106610
const_item_cache= FALSE;

sql/sp.cc

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,8 @@ db_find_routine_aux(THD *thd, stored_procedure_type type, const sp_name *name,
487487
*/
488488
if (name->m_name.length > table->field[1]->field_length)
489489
DBUG_RETURN(SP_KEY_NOT_FOUND);
490-
table->field[0]->store(name->m_db.str, name->m_db.length, &my_charset_bin);
491-
table->field[1]->store(name->m_name.str, name->m_name.length,
492-
&my_charset_bin);
490+
table->field[0]->store(name->m_db, &my_charset_bin);
491+
table->field[1]->store(name->m_name, &my_charset_bin);
493492
table->field[2]->store((longlong) type, TRUE);
494493
key_copy(key, table->record[0], table->key_info,
495494
table->key_info->key_length);
@@ -533,7 +532,7 @@ db_find_routine(THD *thd, stored_procedure_type type, const sp_name *name,
533532
const char *definer;
534533
longlong created;
535534
longlong modified;
536-
st_sp_chistics chistics;
535+
Sp_chistics chistics;
537536
char *ptr;
538537
uint length;
539538
char buff[65];
@@ -567,7 +566,6 @@ db_find_routine(THD *thd, stored_procedure_type type, const sp_name *name,
567566
goto done;
568567
}
569568

570-
bzero((char *)&chistics, sizeof(chistics));
571569
if ((ptr= get_field(thd->mem_root,
572570
table->field[MYSQL_PROC_FIELD_ACCESS])) == NULL)
573571
{
@@ -849,7 +847,7 @@ db_load_routine(THD *thd, stored_procedure_type type,
849847
params, strlen(params),
850848
returns, strlen(returns),
851849
body, strlen(body),
852-
&chistics, definer_user_name, definer_host_name,
850+
chistics, definer_user_name, definer_host_name,
853851
sql_mode))
854852
{
855853
ret= SP_INTERNAL_ERROR;
@@ -902,7 +900,7 @@ db_load_routine(THD *thd, stored_procedure_type type,
902900
}
903901

904902
(*sphp)->set_definer(definer_user_name, definer_host_name);
905-
(*sphp)->set_info(created, modified, &chistics, sql_mode);
903+
(*sphp)->set_info(created, modified, chistics, sql_mode);
906904
(*sphp)->set_creation_ctx(creation_ctx);
907905
(*sphp)->optimize();
908906
/*
@@ -1139,41 +1137,41 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp)
11391137

11401138
store_failed=
11411139
table->field[MYSQL_PROC_FIELD_DB]->
1142-
store(sp->m_db.str, sp->m_db.length, system_charset_info);
1140+
store(sp->m_db, system_charset_info);
11431141

11441142
store_failed= store_failed ||
11451143
table->field[MYSQL_PROC_FIELD_NAME]->
1146-
store(sp->m_name.str, sp->m_name.length, system_charset_info);
1144+
store(sp->m_name, system_charset_info);
11471145

11481146
store_failed= store_failed ||
11491147
table->field[MYSQL_PROC_MYSQL_TYPE]->
11501148
store((longlong)type, TRUE);
11511149

11521150
store_failed= store_failed ||
11531151
table->field[MYSQL_PROC_FIELD_SPECIFIC_NAME]->
1154-
store(sp->m_name.str, sp->m_name.length, system_charset_info);
1152+
store(sp->m_name, system_charset_info);
11551153

1156-
if (sp->m_chistics->daccess != SP_DEFAULT_ACCESS)
1154+
if (sp->daccess() != SP_DEFAULT_ACCESS)
11571155
{
11581156
store_failed= store_failed ||
11591157
table->field[MYSQL_PROC_FIELD_ACCESS]->
1160-
store((longlong)sp->m_chistics->daccess, TRUE);
1158+
store((longlong)sp->daccess(), TRUE);
11611159
}
11621160

11631161
store_failed= store_failed ||
11641162
table->field[MYSQL_PROC_FIELD_DETERMINISTIC]->
1165-
store((longlong)(sp->m_chistics->detistic ? 1 : 2), TRUE);
1163+
store((longlong)(sp->detistic() ? 1 : 2), TRUE);
11661164

1167-
if (sp->m_chistics->suid != SP_IS_DEFAULT_SUID)
1165+
if (sp->suid() != SP_IS_DEFAULT_SUID)
11681166
{
11691167
store_failed= store_failed ||
11701168
table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]->
1171-
store((longlong)sp->m_chistics->suid, TRUE);
1169+
store((longlong)sp->suid(), TRUE);
11721170
}
11731171

11741172
store_failed= store_failed ||
11751173
table->field[MYSQL_PROC_FIELD_PARAM_LIST]->
1176-
store(sp->m_params.str, sp->m_params.length, system_charset_info);
1174+
store(sp->m_params, system_charset_info);
11771175

11781176
if (sp->m_type == TYPE_ENUM_FUNCTION)
11791177
{
@@ -1186,11 +1184,11 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp)
11861184

11871185
store_failed= store_failed ||
11881186
table->field[MYSQL_PROC_FIELD_BODY]->
1189-
store(sp->m_body.str, sp->m_body.length, system_charset_info);
1187+
store(sp->m_body, system_charset_info);
11901188

11911189
store_failed= store_failed ||
11921190
table->field[MYSQL_PROC_FIELD_DEFINER]->
1193-
store(definer.str, definer.length, system_charset_info);
1191+
store(definer, system_charset_info);
11941192

11951193
((Field_timestamp *)table->field[MYSQL_PROC_FIELD_CREATED])->set_time();
11961194
((Field_timestamp *)table->field[MYSQL_PROC_FIELD_MODIFIED])->set_time();
@@ -1199,26 +1197,25 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp)
11991197
table->field[MYSQL_PROC_FIELD_SQL_MODE]->
12001198
store((longlong)saved_mode, TRUE);
12011199

1202-
if (sp->m_chistics->comment.str)
1200+
if (sp->comment().str)
12031201
{
12041202
store_failed= store_failed ||
12051203
table->field[MYSQL_PROC_FIELD_COMMENT]->
1206-
store(sp->m_chistics->comment.str, sp->m_chistics->comment.length,
1207-
system_charset_info);
1204+
store(sp->comment(), system_charset_info);
12081205
}
12091206

12101207
if ((sp->m_type == TYPE_ENUM_FUNCTION) &&
12111208
!trust_function_creators && mysql_bin_log.is_open())
12121209
{
1213-
if (!sp->m_chistics->detistic)
1210+
if (!sp->detistic())
12141211
{
12151212
/*
12161213
Note that this test is not perfect; one could use
12171214
a non-deterministic read-only function in an update statement.
12181215
*/
12191216
enum enum_sp_data_access access=
1220-
(sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ?
1221-
SP_DEFAULT_ACCESS_MAPPING : sp->m_chistics->daccess;
1217+
(sp->daccess() == SP_DEFAULT_ACCESS) ?
1218+
SP_DEFAULT_ACCESS_MAPPING : sp->daccess();
12221219
if (access == SP_CONTAINS_SQL ||
12231220
access == SP_MODIFIES_SQL_DATA)
12241221
{
@@ -1255,7 +1252,7 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp)
12551252
table->field[MYSQL_PROC_FIELD_BODY_UTF8]->set_notnull();
12561253
store_failed= store_failed ||
12571254
table->field[MYSQL_PROC_FIELD_BODY_UTF8]->store(
1258-
sp->m_body_utf8.str, sp->m_body_utf8.length, system_charset_info);
1255+
sp->m_body_utf8, system_charset_info);
12591256

12601257
if (store_failed)
12611258
{
@@ -1291,7 +1288,7 @@ sp_create_routine(THD *thd, stored_procedure_type type, sp_head *sp)
12911288
sp->m_params.str, sp->m_params.length,
12921289
retstr.ptr(), retstr.length(),
12931290
sp->m_body.str, sp->m_body.length,
1294-
sp->m_chistics, &(thd->lex->definer->user),
1291+
sp->chistics(), &(thd->lex->definer->user),
12951292
&(thd->lex->definer->host),
12961293
saved_mode))
12971294
{
@@ -1448,8 +1445,7 @@ sp_update_routine(THD *thd, stored_procedure_type type, const sp_name *name,
14481445
table->field[MYSQL_PROC_FIELD_ACCESS]->
14491446
store((longlong)chistics->daccess, TRUE);
14501447
if (chistics->comment.str)
1451-
table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment.str,
1452-
chistics->comment.length,
1448+
table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment,
14531449
system_charset_info);
14541450
if ((ret= table->file->ha_update_row(table->record[1],table->record[0])) &&
14551451
ret != HA_ERR_RECORD_IS_THE_SAME)
@@ -1792,7 +1788,7 @@ sp_find_routine(THD *thd, stored_procedure_type type, const sp_name *name,
17921788
}
17931789
if (db_load_routine(thd, type, name, &new_sp,
17941790
sp->m_sql_mode, sp->m_params.str, returns,
1795-
sp->m_body.str, *sp->m_chistics,
1791+
sp->m_body.str, sp->chistics(),
17961792
&sp->m_definer.user, &sp->m_definer.host,
17971793
sp->m_created, sp->m_modified,
17981794
sp->get_creation_ctx()) == SP_OK)
@@ -2201,15 +2197,15 @@ show_create_sp(THD *thd, String *buf,
22012197
const char *params, ulong paramslen,
22022198
const char *returns, ulong returnslen,
22032199
const char *body, ulong bodylen,
2204-
const st_sp_chistics *chistics,
2200+
const st_sp_chistics &chistics,
22052201
const LEX_CSTRING *definer_user,
22062202
const LEX_CSTRING *definer_host,
22072203
sql_mode_t sql_mode)
22082204
{
22092205
sql_mode_t old_sql_mode= thd->variables.sql_mode;
22102206
/* Make some room to begin with */
22112207
if (buf->alloc(100 + dblen + 1 + namelen + paramslen + returnslen + bodylen +
2212-
chistics->comment.length + 10 /* length of " DEFINER= "*/ +
2208+
chistics.comment.length + 10 /* length of " DEFINER= "*/ +
22132209
USER_HOST_BUFF_SIZE))
22142210
return FALSE;
22152211

@@ -2243,7 +2239,7 @@ show_create_sp(THD *thd, String *buf,
22432239
buf->append(returns, returnslen);
22442240
}
22452241
buf->append('\n');
2246-
switch (chistics->daccess) {
2242+
switch (chistics.daccess) {
22472243
case SP_NO_SQL:
22482244
buf->append(STRING_WITH_LEN(" NO SQL\n"));
22492245
break;
@@ -2258,14 +2254,14 @@ show_create_sp(THD *thd, String *buf,
22582254
/* Do nothing */
22592255
break;
22602256
}
2261-
if (chistics->detistic)
2257+
if (chistics.detistic)
22622258
buf->append(STRING_WITH_LEN(" DETERMINISTIC\n"));
2263-
if (chistics->suid == SP_IS_NOT_SUID)
2259+
if (chistics.suid == SP_IS_NOT_SUID)
22642260
buf->append(STRING_WITH_LEN(" SQL SECURITY INVOKER\n"));
2265-
if (chistics->comment.length)
2261+
if (chistics.comment.length)
22662262
{
22672263
buf->append(STRING_WITH_LEN(" COMMENT "));
2268-
append_unescaped(buf, chistics->comment.str, chistics->comment.length);
2264+
append_unescaped(buf, chistics.comment.str, chistics.comment.length);
22692265
buf->append('\n');
22702266
}
22712267
buf->append(body, bodylen);
@@ -2303,7 +2299,6 @@ sp_load_for_information_schema(THD *thd, TABLE *proc_table, String *db,
23032299
{
23042300
const char *sp_body;
23052301
String defstr;
2306-
struct st_sp_chistics sp_chistics;
23072302
const LEX_CSTRING definer_user= {STRING_WITH_LEN("")};
23082303
const LEX_CSTRING definer_host= {STRING_WITH_LEN("")};
23092304
LEX_CSTRING sp_db_str;
@@ -2326,15 +2321,14 @@ sp_load_for_information_schema(THD *thd, TABLE *proc_table, String *db,
23262321
Stored_program_creation_ctx *creation_ctx=
23272322
Stored_routine_creation_ctx::load_from_db(thd, &sp_name_obj, proc_table);
23282323
sp_body= (type == TYPE_ENUM_FUNCTION ? "RETURN NULL" : "BEGIN END");
2329-
bzero((char*) &sp_chistics, sizeof(sp_chistics));
23302324
defstr.set_charset(creation_ctx->get_client_cs());
23312325
if (!show_create_sp(thd, &defstr, type,
23322326
sp_db_str.str, sp_db_str.length,
23332327
sp_name_obj.m_name.str, sp_name_obj.m_name.length,
23342328
params, strlen(params),
23352329
returns, strlen(returns),
23362330
sp_body, strlen(sp_body),
2337-
&sp_chistics, &definer_user, &definer_host, sql_mode))
2331+
Sp_chistics(), &definer_user, &definer_host, sql_mode))
23382332
return 0;
23392333

23402334
thd->lex= &newlex;

sql/sp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ bool show_create_sp(THD *thd, String *buf,
240240
const char *params, ulong paramslen,
241241
const char *returns, ulong returnslen,
242242
const char *body, ulong bodylen,
243-
const st_sp_chistics *chistics,
243+
const st_sp_chistics &chistics,
244244
const LEX_CSTRING *definer_user,
245245
const LEX_CSTRING *definer_host,
246246
sql_mode_t sql_mode);

sql/sp_head.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ sp_head::sp_head(stored_procedure_type type)
559559
m_sp_cache_version(0),
560560
m_creation_ctx(0),
561561
unsafe_flags(0),
562+
m_created(0),
563+
m_modified(0),
562564
m_recursion_level(0),
563565
m_next_cached_sp(0),
564566
m_param_begin(NULL),
@@ -1408,7 +1410,7 @@ set_routine_security_ctx(THD *thd, sp_head *sp, bool is_proc,
14081410
Security_context **save_ctx)
14091411
{
14101412
*save_ctx= 0;
1411-
if (sp->m_chistics->suid != SP_IS_NOT_SUID &&
1413+
if (sp->suid() != SP_IS_NOT_SUID &&
14121414
sp->m_security_ctx.change_security_context(thd, &sp->m_definer.user,
14131415
&sp->m_definer.host,
14141416
&sp->m_db,
@@ -1520,7 +1522,7 @@ sp_head::execute_trigger(THD *thd,
15201522
Security_context *save_ctx= NULL;
15211523

15221524

1523-
if (m_chistics->suid != SP_IS_NOT_SUID &&
1525+
if (suid() != SP_IS_NOT_SUID &&
15241526
m_security_ctx.change_security_context(thd,
15251527
&m_definer.user,
15261528
&m_definer.host,
@@ -2432,20 +2434,26 @@ sp_head::sp_add_instr_cpush_for_cursors(THD *thd, sp_pcontext *pcontext)
24322434
}
24332435

24342436

2437+
void
2438+
sp_head::set_chistics(const st_sp_chistics &chistics)
2439+
{
2440+
m_chistics.set(chistics);
2441+
if (m_chistics.comment.length == 0)
2442+
m_chistics.comment.str= 0;
2443+
else
2444+
m_chistics.comment.str= strmake_root(mem_root,
2445+
m_chistics.comment.str,
2446+
m_chistics.comment.length);
2447+
}
2448+
2449+
24352450
void
24362451
sp_head::set_info(longlong created, longlong modified,
2437-
const st_sp_chistics *chistics, sql_mode_t sql_mode)
2452+
const st_sp_chistics &chistics, sql_mode_t sql_mode)
24382453
{
24392454
m_created= created;
24402455
m_modified= modified;
2441-
m_chistics= (st_sp_chistics *) memdup_root(mem_root, (char*) chistics,
2442-
sizeof(*chistics));
2443-
if (m_chistics->comment.length == 0)
2444-
m_chistics->comment.str= 0;
2445-
else
2446-
m_chistics->comment.str= strmake_root(mem_root,
2447-
m_chistics->comment.str,
2448-
m_chistics->comment.length);
2456+
set_chistics(chistics);
24492457
m_sql_mode= sql_mode;
24502458
}
24512459

0 commit comments

Comments
 (0)