Skip to content

Commit bef61e5

Browse files
author
Catalin Besleaga
committed
Bug#35395333: Follow-up on Bug#36402968
Follow-up on Bug#36402968: renamed context to m_name_resolution_ctx in Item_func_current_user and in Item_func_sp, initialized field in Name_resolution_context and removed the constructor and the init method. Change-Id: I6a86d9164f4d9bd50649032e60d2e18c217ce91c
1 parent 3cd6be6 commit bef61e5

9 files changed

+60
-71
lines changed

sql/item.h

+18-35
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class Item_name_string : public Name_string {
390390
bool is_autogenerated_arg);
391391
};
392392

393-
/*
393+
/**
394394
Instances of Name_resolution_context store the information necessary for
395395
name resolution of Items and other context analysis of a query made in
396396
fix_fields().
@@ -400,57 +400,57 @@ class Item_name_string : public Name_string {
400400
(sql_yacc.yy)), but the structure itself will be initialized after parsing
401401
is complete
402402
403-
TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
403+
@todo move subquery of INSERT ... SELECT and CREATE ... SELECT to
404404
separate Query_block which allow to remove tricks of changing this
405405
structure before and after INSERT/CREATE and its SELECT to make correct
406406
field name resolution.
407407
*/
408408
struct Name_resolution_context {
409-
/*
409+
/**
410410
The name resolution context to search in when an Item cannot be
411411
resolved in this context (the context of an outer select)
412412
*/
413-
Name_resolution_context *outer_context;
413+
Name_resolution_context *outer_context{nullptr};
414414
/// Link to next name res context with the same query block as the base
415-
Name_resolution_context *next_context;
415+
Name_resolution_context *next_context{nullptr};
416416

417-
/*
417+
/**
418418
List of tables used to resolve the items of this context. Usually these
419419
are tables from the FROM clause of SELECT statement. The exceptions are
420420
INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
421421
subquery is not moved to a separate Query_block. For these types of
422422
statements we have to change this member dynamically to ensure correct
423423
name resolution of different parts of the statement.
424424
*/
425-
Table_ref *table_list;
426-
/*
425+
Table_ref *table_list{nullptr};
426+
/**
427427
In most cases the two table references below replace 'table_list' above
428428
for the purpose of name resolution. The first and last name resolution
429429
table references allow us to search only in a sub-tree of the nested
430430
join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
431431
and JOIN ... ON.
432432
*/
433-
Table_ref *first_name_resolution_table;
434-
/*
433+
Table_ref *first_name_resolution_table{nullptr};
434+
/**
435435
Last table to search in the list of leaf table references that begins
436436
with first_name_resolution_table.
437437
*/
438-
Table_ref *last_name_resolution_table;
438+
Table_ref *last_name_resolution_table{nullptr};
439439

440-
/*
440+
/**
441441
Query_block item belong to, in case of merged VIEW it can differ from
442442
Query_block where item was created, so we can't use table_list/field_list
443443
from there
444444
*/
445-
Query_block *query_block;
445+
Query_block *query_block{nullptr};
446446

447447
/*
448448
Processor of errors caused during Item name resolving, now used only to
449449
hide underlying tables in errors about views (i.e. it substitute some
450450
errors for views)
451451
*/
452-
bool view_error_handler;
453-
Table_ref *view_error_handler_arg;
452+
bool view_error_handler{false};
453+
Table_ref *view_error_handler_arg{nullptr};
454454

455455
/**
456456
When true, items are resolved in this context against
@@ -460,30 +460,13 @@ struct Name_resolution_context {
460460
461461
@see Query_block::item_list, Query_block::group_list
462462
*/
463-
bool resolve_in_select_list;
463+
bool resolve_in_select_list{false};
464464

465-
/*
465+
/**
466466
Security context of this name resolution context. It's used for views
467467
and is non-zero only if the view is defined with SQL SECURITY DEFINER.
468468
*/
469-
Security_context *security_ctx;
470-
471-
Name_resolution_context()
472-
: outer_context(nullptr),
473-
next_context(nullptr),
474-
table_list(nullptr),
475-
query_block(nullptr),
476-
view_error_handler_arg(nullptr),
477-
security_ctx(nullptr) {
478-
DBUG_PRINT("outer_field", ("creating ctx %p", this));
479-
}
480-
481-
void init() {
482-
resolve_in_select_list = false;
483-
view_error_handler = false;
484-
first_name_resolution_table = nullptr;
485-
last_name_resolution_table = nullptr;
486-
}
469+
Security_context *security_ctx{nullptr};
487470

488471
void resolve_in_table_list_only(Table_ref *tables) {
489472
table_list = first_name_resolution_table = tables;

sql/item_func.cc

+11-8
Original file line numberDiff line numberDiff line change
@@ -8132,7 +8132,7 @@ bool Item_func_sp::do_itemize(Parse_context *pc, Item **res) {
81328132
THD *thd = pc->thd;
81338133
LEX *lex = thd->lex;
81348134

8135-
context = lex->current_context();
8135+
m_name_resolution_ctx = lex->current_context();
81368136
lex->safe_to_cache_query = false;
81378137

81388138
if (m_name->m_db.str == nullptr) {
@@ -8223,7 +8223,8 @@ bool Item_func_sp::init_result_field(THD *thd) {
82238223
assert(sp_result_field == nullptr);
82248224

82258225
Internal_error_handler_holder<View_error_handler, Table_ref> view_handler(
8226-
thd, context->view_error_handler, context->view_error_handler_arg);
8226+
thd, m_name_resolution_ctx->view_error_handler,
8227+
m_name_resolution_ctx->view_error_handler_arg);
82278228
m_sp = sp_find_routine(thd, enum_sp_type::FUNCTION, m_name,
82288229
&thd->sp_func_cache, true);
82298230
if (m_sp == nullptr) {
@@ -8349,7 +8350,8 @@ bool Item_func_sp::execute() {
83498350
THD *thd = current_thd;
83508351

83518352
Internal_error_handler_holder<View_error_handler, Table_ref> view_handler(
8352-
thd, context->view_error_handler, context->view_error_handler_arg);
8353+
thd, m_name_resolution_ctx->view_error_handler,
8354+
m_name_resolution_ctx->view_error_handler_arg);
83538355

83548356
// Bind to an instance of the stored function:
83558357
if (m_sp == nullptr) {
@@ -8398,9 +8400,9 @@ bool Item_func_sp::execute_impl(THD *thd) {
83988400

83998401
DBUG_TRACE;
84008402

8401-
if (context->security_ctx) {
8403+
if (m_name_resolution_ctx->security_ctx != nullptr) {
84028404
/* Set view definer security context */
8403-
thd->set_security_context(context->security_ctx);
8405+
thd->set_security_context(m_name_resolution_ctx->security_ctx);
84048406
}
84058407
if (sp_check_access(thd)) goto error;
84068408

@@ -8505,17 +8507,18 @@ bool Item_func_sp::fix_fields(THD *thd, Item **ref) {
85058507
*/
85068508
if (!thd->lex->is_view_context_analysis() ||
85078509
(thd->lex->sql_command == SQLCOM_CREATE_VIEW)) {
8508-
if (context->security_ctx) {
8510+
if (m_name_resolution_ctx->security_ctx != nullptr) {
85098511
/* Set view definer security context */
8510-
thd->set_security_context(context->security_ctx);
8512+
thd->set_security_context(m_name_resolution_ctx->security_ctx);
85118513
}
85128514

85138515
/*
85148516
Check whether user has execute privilege or not
85158517
*/
85168518

85178519
Internal_error_handler_holder<View_error_handler, Table_ref> view_handler(
8518-
thd, context->view_error_handler, context->view_error_handler_arg);
8520+
thd, m_name_resolution_ctx->view_error_handler,
8521+
m_name_resolution_ctx->view_error_handler_arg);
85198522

85208523
const bool res = check_routine_access(thd, EXECUTE_ACL, m_name->m_db.str,
85218524
m_name->m_name.str, false, false);

sql/item_func.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,9 @@ class Item_func_sp final : public Item_func {
38943894
typedef Item_func super;
38953895

38963896
private:
3897-
Name_resolution_context *context{nullptr};
3897+
/// Holds the security definer context(if defined with SQL SECURITY DEFINER)
3898+
/// and the error the handler.
3899+
Name_resolution_context *m_name_resolution_ctx{nullptr};
38983900
/// The name of the stored function
38993901
sp_name *m_name{nullptr};
39003902
/// Pointer to actual function instance (null when not resolved or executing)
@@ -3942,7 +3944,8 @@ class Item_func_sp final : public Item_func {
39423944
bool val_json(Json_wrapper *result) override;
39433945

39443946
bool change_context_processor(uchar *arg) override {
3945-
context = reinterpret_cast<Item_ident::Change_context *>(arg)->m_context;
3947+
m_name_resolution_ctx =
3948+
pointer_cast<Item_ident::Change_context *>(arg)->m_context;
39463949
return false;
39473950
}
39483951

sql/item_strfunc.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -2004,32 +2004,32 @@ bool Item_func_current_user::resolve_type(THD *thd) {
20042004
if (super::resolve_type(thd)) {
20052005
return true;
20062006
}
2007-
if (context->security_ctx == nullptr) {
2007+
if (m_name_resolution_ctx->security_ctx == nullptr) {
20082008
return false;
20092009
}
20102010

20112011
// If Name_resolution_context has a definer Security_context priv_user and
20122012
// priv_host from it are copied into the item since the
20132013
// Name_resolution_context may have been deallocated when val_str() gets
20142014
// called.
2015-
LEX_CSTRING pu = context->security_ctx->priv_user();
2015+
LEX_CSTRING pu = m_name_resolution_ctx->security_ctx->priv_user();
20162016
if (pu.str != nullptr) {
2017-
definer_priv_user = LexStringDupRoot(thd->mem_root, pu);
2018-
if (definer_priv_user.str == nullptr) return true;
2017+
m_definer_priv_user = LexStringDupRoot(thd->mem_root, pu);
2018+
if (m_definer_priv_user.str == nullptr) return true;
20192019
}
2020-
LEX_CSTRING ph = context->security_ctx->priv_host();
2020+
LEX_CSTRING ph = m_name_resolution_ctx->security_ctx->priv_host();
20212021
if (ph.str != nullptr) {
2022-
definer_priv_host = LexStringDupRoot(thd->mem_root, ph);
2023-
if (definer_priv_host.str == nullptr) return true;
2022+
m_definer_priv_host = LexStringDupRoot(thd->mem_root, ph);
2023+
if (m_definer_priv_host.str == nullptr) return true;
20242024
}
20252025
return false;
20262026
}
20272027

20282028
String *Item_func_current_user::val_str(String *) {
20292029
assert(fixed);
20302030
if (!m_evaluated) {
2031-
if (definer_priv_user.str != nullptr) {
2032-
if (evaluate(definer_priv_user.str, definer_priv_host.str))
2031+
if (m_definer_priv_user.str != nullptr) {
2032+
if (evaluate(m_definer_priv_user.str, m_definer_priv_host.str))
20332033
return nullptr;
20342034
} else {
20352035
Security_context *const ctx = current_thd->security_context();
@@ -2081,7 +2081,7 @@ bool Item_func_current_user::do_itemize(Parse_context *pc, Item **res) {
20812081
if (skip_itemize(res)) return false;
20822082
if (super::do_itemize(pc, res)) return true;
20832083

2084-
context = pc->thd->lex->current_context();
2084+
m_name_resolution_ctx = pc->thd->lex->current_context();
20852085
return false;
20862086
}
20872087

sql/item_strfunc.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -676,23 +676,23 @@ class Item_func_user : public Item_func_sysconst {
676676

677677
class Item_func_current_user : public Item_func_user {
678678
typedef Item_func_user super;
679-
/*
679+
/**
680680
Used to pass a security context to the resolver functions.
681681
Only used for definer views. In all other contexts, the security context
682682
passed here is nullptr and is instead looked up dynamically at run time
683683
from the current THD.
684684
*/
685-
Name_resolution_context *context = nullptr;
685+
Name_resolution_context *m_name_resolution_ctx = nullptr;
686686

687-
// Copied from context in fix_fields if definer Security_context
688-
// is set in Name_resolution_context
689-
LEX_CSTRING definer_priv_user = {};
690-
LEX_CSTRING definer_priv_host = {};
687+
/// Copied from m_name_resolution_ctx in fix_fields if the definer
688+
/// Security_context is set in Name_resolution_context
689+
LEX_CSTRING m_definer_priv_user = {};
690+
LEX_CSTRING m_definer_priv_host = {};
691691

692692
protected:
693693
type_conversion_status save_in_field_inner(Field *field, bool) override;
694694

695-
// Overridden to copy definer priv_user and priv_host
695+
/// Overridden to copy definer priv_user and priv_host
696696
bool resolve_type(THD *) override;
697697

698698
public:

sql/sql_base.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -8520,10 +8520,12 @@ static bool test_if_string_in_list(const char *find, List<String> *str_list) {
85208520

85218521
static bool set_new_item_local_context(THD *thd, Item_ident *item,
85228522
Table_ref *table_ref) {
8523-
Name_resolution_context *context;
8524-
if (!(context = new (thd->mem_root) Name_resolution_context))
8525-
return true; /* purecov: inspected */
8526-
context->init();
8523+
Name_resolution_context *context =
8524+
new (thd->mem_root) Name_resolution_context;
8525+
if (context == nullptr) {
8526+
/* purecov: inspected */
8527+
return true;
8528+
}
85278529
context->first_name_resolution_table = context->last_name_resolution_table =
85288530
table_ref;
85298531
context->query_block = table_ref->query_block;

sql/sql_lex.cc

-1
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,6 @@ Query_block::Query_block(MEM_ROOT *mem_root, Item *where, Item *having)
22712271
*/
22722272

22732273
bool Query_block::set_context(Name_resolution_context *outer_context) {
2274-
context.init();
22752274
context.query_block = this;
22762275
context.outer_context = outer_context;
22772276
/*

sql/sql_parse.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -6307,9 +6307,9 @@ void Query_block::set_lock_for_tables(thr_lock_type lock_type) {
63076307
bool push_new_name_resolution_context(Parse_context *pc, Table_ref *left_op,
63086308
Table_ref *right_op) {
63096309
THD *thd = pc->thd;
6310-
Name_resolution_context *on_context;
6311-
if (!(on_context = new (thd->mem_root) Name_resolution_context)) return true;
6312-
on_context->init();
6310+
Name_resolution_context *on_context =
6311+
new (thd->mem_root) Name_resolution_context;
6312+
if (on_context == nullptr) return true;
63136313
on_context->first_name_resolution_table =
63146314
left_op->first_leaf_for_name_resolution();
63156315
on_context->last_name_resolution_table =

sql/sql_resolver.cc

-1
Original file line numberDiff line numberDiff line change
@@ -6123,7 +6123,6 @@ bool Query_block::transform_grouped_to_derived(THD *thd, bool *break_off) {
61236123
context.table_list = tl;
61246124
context.first_name_resolution_table = tl;
61256125
assert(context.last_name_resolution_table == nullptr);
6126-
new_derived->context.init();
61276126
new_derived->context.table_list = get_table_list();
61286127
new_derived->context.query_block = new_derived;
61296128
new_derived->context.outer_context = &context;

0 commit comments

Comments
 (0)