Skip to content
Permalink
Browse files
add const qualifiers to sys_var::value_ptr functions and fix const casts
This is important since Sys_var_typelib and its descendants return
pointers to constant symbols from *_value_ptr, which are situated in
write-protected-memory.

* functions const-qualified:
  - value_ptr
  - session_value_ptr
  - global_value_ptr
  - default_value_ptr
  - Sys_var_vers_asof::value_ptr
  - other minor private ones

* remove C-style typecasts when it discards qualifiers
  • Loading branch information
FooBarrior committed Jul 27, 2021
1 parent c6bff46 commit 6ed4750
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 163 deletions.
@@ -5657,7 +5657,7 @@ void Item_func_get_system_var::update_null_value()

bool Item_func_get_system_var::fix_length_and_dec()
{
char *cptr;
const char *cptr;
maybe_null= TRUE;
max_length= 0;

@@ -5691,9 +5691,12 @@ bool Item_func_get_system_var::fix_length_and_dec()
case SHOW_CHAR:
case SHOW_CHAR_PTR:
mysql_mutex_lock(&LOCK_global_system_variables);
cptr= var->show_type() == SHOW_CHAR ?
(char*) var->value_ptr(current_thd, var_type, &component) :
*(char**) var->value_ptr(current_thd, var_type, &component);
cptr= var->show_type() == SHOW_CHAR ?
reinterpret_cast<const char*>(var->value_ptr(current_thd, var_type,
&component)) :
*reinterpret_cast<const char* const*>(var->value_ptr(current_thd,
var_type,
&component));
if (cptr)
max_length= (uint32)system_charset_info->cset->numchars(system_charset_info,
cptr,
@@ -5706,7 +5709,10 @@ bool Item_func_get_system_var::fix_length_and_dec()
case SHOW_LEX_STRING:
{
mysql_mutex_lock(&LOCK_global_system_variables);
LEX_STRING *ls= ((LEX_STRING*)var->value_ptr(current_thd, var_type, &component));
const LEX_STRING *ls=
reinterpret_cast<const LEX_STRING*>(var->value_ptr(current_thd,
var_type,
&component));
max_length= (uint32)system_charset_info->cset->numchars(system_charset_info,
ls->str,
ls->str + ls->length);
@@ -233,12 +233,12 @@ bool sys_var::update(THD *thd, set_var *var)
}
}

uchar *sys_var::session_value_ptr(THD *thd, const LEX_CSTRING *base)
const uchar *sys_var::session_value_ptr(THD *thd, const LEX_CSTRING *base) const
{
return session_var_ptr(thd);
}

uchar *sys_var::global_value_ptr(THD *thd, const LEX_CSTRING *base)
const uchar *sys_var::global_value_ptr(THD *thd, const LEX_CSTRING *base) const
{
return global_var_ptr();
}
@@ -271,8 +271,8 @@ bool sys_var::check(THD *thd, set_var *var)
return false;
}

uchar *sys_var::value_ptr(THD *thd, enum_var_type type,
const LEX_CSTRING *base)
const uchar *sys_var::value_ptr(THD *thd, enum_var_type type,
const LEX_CSTRING *base) const
{
DBUG_ASSERT(base);
if (type == OPT_GLOBAL || scope() == GLOBAL)
@@ -510,7 +510,7 @@ bool throw_bounds_warning(THD *thd, const char *name, bool fixed, double v)
return false;
}

CHARSET_INFO *sys_var::charset(THD *thd)
CHARSET_INFO *sys_var::charset(THD *thd) const
{
return is_os_charset ? thd->variables.character_set_filesystem :
system_charset_info;
@@ -1047,7 +1047,7 @@ int set_var_collation_client::update(THD *thd)
INFORMATION_SCHEMA.SYSTEM_VARIABLES
*****************************************************************************/
static void store_value_ptr(Field *field, sys_var *var, String *str,
uchar *value_ptr)
const uchar *value_ptr)
{
field->set_notnull();
str= var->val_str_nolock(str, field->table->in_use, value_ptr);
@@ -1115,8 +1115,8 @@ int fill_sysvars(THD *thd, TABLE_LIST *tables, COND *cond)
fields[3]->store(origin->str, origin->length, scs);

// DEFAULT_VALUE
uchar *def= var->is_readonly() && var->option.id < 0
? 0 : var->default_value_ptr(thd);
const uchar *def= var->is_readonly() && var->option.id < 0
? 0 : var->default_value_ptr(thd);
if (def)
store_value_ptr(fields[4], var, &strbuf, def);

@@ -112,7 +112,7 @@ class sys_var: protected Value_source // for double_from_string_with_check
virtual sys_var_pluginvar *cast_pluginvar() { return 0; }

bool check(THD *thd, set_var *var);
uchar *value_ptr(THD *thd, enum_var_type type, const LEX_CSTRING *base);
const uchar *value_ptr(THD *thd, enum_var_type type, const LEX_CSTRING *base) const;

/**
Update the system variable with the default value from either
@@ -127,9 +127,9 @@ class sys_var: protected Value_source // for double_from_string_with_check
String *val_str(String *str, THD *thd, enum_var_type type, const LEX_CSTRING *base);
double val_real(bool *is_null, THD *thd, enum_var_type type, const LEX_CSTRING *base);

SHOW_TYPE show_type() { return show_val_type; }
SHOW_TYPE show_type() const { return show_val_type; }
int scope() const { return flags & SCOPE_MASK; }
CHARSET_INFO *charset(THD *thd);
CHARSET_INFO *charset(THD *thd) const;
bool is_readonly() const { return flags & READONLY; }
/**
the following is only true for keycache variables,
@@ -208,7 +208,7 @@ class sys_var: protected Value_source // for double_from_string_with_check
*/
virtual bool session_is_default(THD *thd) { return false; }

virtual uchar *default_value_ptr(THD *thd)
virtual const uchar *default_value_ptr(THD *thd) const
{ return (uchar*)&option.def_value; }

private:
@@ -230,18 +230,18 @@ class sys_var: protected Value_source // for double_from_string_with_check
It must be of show_val_type type (my_bool for SHOW_MY_BOOL,
int for SHOW_INT, longlong for SHOW_LONGLONG, etc).
*/
virtual uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base);
virtual uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base);
virtual const uchar *session_value_ptr(THD *thd, const LEX_CSTRING *base) const;
virtual const uchar *global_value_ptr(THD *thd, const LEX_CSTRING *base) const;

/**
A pointer to a storage area of the variable, to the raw data.
Typically it's the same as session_value_ptr(), but it's different,
for example, for ENUM, that is printed as a string, but stored as a number.
*/
uchar *session_var_ptr(THD *thd)
uchar *session_var_ptr(THD *thd) const
{ return ((uchar*)&(thd->variables)) + offset; }

uchar *global_var_ptr()
uchar *global_var_ptr() const
{ return ((uchar*)&global_system_variables) + offset; }

void *max_var_ptr()
@@ -293,14 +293,14 @@ class sys_var_pluginvar: public sys_var, public Sql_alloc
sys_var_pluginvar(sys_var_chain *chain, const char *name_arg,
st_plugin_int *p, st_mysql_sys_var *plugin_var_arg);
sys_var_pluginvar *cast_pluginvar() { return this; }
uchar* real_value_ptr(THD *thd, enum_var_type type);
TYPELIB* plugin_var_typelib(void);
uchar* do_value_ptr(THD *thd, enum_var_type type, const LEX_CSTRING *base);
uchar* session_value_ptr(THD *thd, const LEX_CSTRING *base)
uchar* real_value_ptr(THD *thd, enum_var_type type) const;
TYPELIB* plugin_var_typelib(void) const;
const uchar* do_value_ptr(THD *thd, enum_var_type type, const LEX_CSTRING *base) const;
const uchar* session_value_ptr(THD *thd, const LEX_CSTRING *base) const
{ return do_value_ptr(thd, OPT_SESSION, base); }
uchar* global_value_ptr(THD *thd, const LEX_CSTRING *base)
const uchar* global_value_ptr(THD *thd, const LEX_CSTRING *base) const
{ return do_value_ptr(thd, OPT_GLOBAL, base); }
uchar *default_value_ptr(THD *thd)
const uchar *default_value_ptr(THD *thd) const
{ return do_value_ptr(thd, OPT_DEFAULT, 0); }
bool do_check(THD *thd, set_var *var);
virtual void session_save_default(THD *thd, set_var *var) {}
@@ -3349,7 +3349,7 @@ sys_var_pluginvar::sys_var_pluginvar(sys_var_chain *chain, const char *name_arg,
plugin_opt_set_limits(&option, pv);
}

uchar* sys_var_pluginvar::real_value_ptr(THD *thd, enum_var_type type)
uchar* sys_var_pluginvar::real_value_ptr(THD *thd, enum_var_type type) const
{
if (type == OPT_DEFAULT)
{
@@ -3423,7 +3423,7 @@ bool sys_var_pluginvar::session_is_default(THD *thd)
}


TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
TYPELIB* sys_var_pluginvar::plugin_var_typelib(void) const
{
switch (plugin_var->flags & (PLUGIN_VAR_TYPEMASK | PLUGIN_VAR_THDLOCAL)) {
case PLUGIN_VAR_ENUM:
@@ -3441,12 +3441,10 @@ TYPELIB* sys_var_pluginvar::plugin_var_typelib(void)
}


uchar* sys_var_pluginvar::do_value_ptr(THD *thd, enum_var_type type,
const LEX_CSTRING *base)
const uchar* sys_var_pluginvar::do_value_ptr(THD *thd, enum_var_type type,
const LEX_CSTRING *base) const
{
uchar* result;

result= real_value_ptr(thd, type);
const uchar* result= real_value_ptr(thd, type);

if ((plugin_var->flags & PLUGIN_VAR_TYPEMASK) == PLUGIN_VAR_ENUM)
result= (uchar*) get_type(plugin_var_typelib(), *(ulong*)result);
@@ -3568,6 +3568,28 @@ void remove_status_vars(SHOW_VAR *list)
}
}

/**
A union holding a pointer to a type that can be referred by a status variable.
*/
union Any_pointer {
const void *as_void;
const uchar *as_uchar;
const char *as_char;
const char ** as_charptr;
const double *as_double;
const int * as_int;
const uint * as_uint;
const long *as_long;
const longlong *as_longlong;
const bool *as_bool;
const my_bool *as_my_bool;
const sys_var *as_sys_var;
const system_status_var *as_system_status_var;
const ha_rows *as_ha_rows;
const LEX_STRING *as_lex_cstring;
const SHOW_COMP_OPTION *as_show_comp_options;
intptr as_intptr;
};

/**
@brief Returns the value of a system or a status variable.
@@ -3592,16 +3614,18 @@ const char* get_one_variable(THD *thd,
const CHARSET_INFO **charset, char *buff,
size_t *length)
{
void *value= variable->value;
Any_pointer value, status_var_value;
value.as_void= variable->value;
status_var_value.as_system_status_var= status_var;
const char *pos= buff;
const char *end= buff;


if (show_type == SHOW_SYS)
{
sys_var *var= (sys_var *) value;
const sys_var *var= value.as_sys_var;
show_type= var->show_type();
value= var->value_ptr(thd, value_type, &null_clex_str);
value.as_uchar= var->value_ptr(thd, value_type, &null_clex_str);
*charset= var->charset(thd);
}

@@ -3611,78 +3635,76 @@ const char* get_one_variable(THD *thd,
*/
switch (show_type) {
case SHOW_DOUBLE_STATUS:
value= ((char *) status_var + (intptr) value);
value.as_char= status_var_value.as_char + value.as_intptr;
/* fall through */
case SHOW_DOUBLE:
/* 6 is the default precision for '%f' in sprintf() */
end= buff + my_fcvt(*(double *) value, 6, buff, NULL);
end= buff + my_fcvt(*value.as_double, 6, buff, NULL);
break;
case SHOW_LONG_STATUS:
value= ((char *) status_var + (intptr) value);
value.as_char= status_var_value.as_char + value.as_intptr;
/* fall through */
case SHOW_ULONG:
case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
end= int10_to_str(*(long*) value, buff, 10);
end= int10_to_str(*value.as_long, buff, 10);
break;
case SHOW_LONGLONG_STATUS:
value= ((char *) status_var + (intptr) value);
value.as_char= status_var_value.as_char + value.as_intptr;
/* fall through */
case SHOW_ULONGLONG:
end= longlong10_to_str(*(longlong*) value, buff, 10);
end= longlong10_to_str(*value.as_longlong, buff, 10);
break;
case SHOW_HA_ROWS:
end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
end= longlong10_to_str((longlong) *value.as_ha_rows, buff, 10);
break;
case SHOW_BOOL:
end= strmov(buff, *(bool*) value ? "ON" : "OFF");
end= strmov(buff, *value.as_bool ? "ON" : "OFF");
break;
case SHOW_MY_BOOL:
end= strmov(buff, *(my_bool*) value ? "ON" : "OFF");
end= strmov(buff, *value.as_my_bool ? "ON" : "OFF");
break;
case SHOW_UINT32_STATUS:
value= ((char *) status_var + (intptr) value);
value.as_char= status_var_value.as_char + value.as_intptr;
/* fall through */
case SHOW_UINT:
end= int10_to_str((long) *(uint*) value, buff, 10);
end= int10_to_str((long) *value.as_uint, buff, 10);
break;
case SHOW_SINT:
end= int10_to_str((long) *(int*) value, buff, -10);
end= int10_to_str((long) *value.as_int, buff, -10);
break;
case SHOW_SLONG:
end= int10_to_str(*(long*) value, buff, -10);
end= int10_to_str(*value.as_long, buff, -10);
break;
case SHOW_SLONGLONG:
end= longlong10_to_str(*(longlong*) value, buff, -10);
end= longlong10_to_str(*value.as_longlong, buff, -10);
break;
case SHOW_HAVE:
{
SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
pos= show_comp_option_name[(int) tmp];
pos= show_comp_option_name[(int) *value.as_show_comp_options];
end= strend(pos);
break;
}
case SHOW_CHAR:
{
if (!(pos= (char*)value))
if (!(pos= value.as_char))
pos= "";
end= strend(pos);
break;
}
case SHOW_CHAR_PTR:
{
if (!(pos= *(char**) value))
if (!(pos= *value.as_charptr))
pos= "";

end= strend(pos);
break;
}
case SHOW_LEX_STRING:
{
LEX_STRING *ls=(LEX_STRING*)value;
if (!(pos= ls->str))
if (!(pos= value.as_lex_cstring->str))
end= pos= "";
else
end= pos + ls->length;
end= pos + value.as_lex_cstring->length;
break;
}
case SHOW_UNDEF:

0 comments on commit 6ed4750

Please sign in to comment.