MDEV-19566 Remove Item::name related strlen() calls in constructors of some Item_string descendands

This commit is contained in:
Alexander Barkov 2019-05-23 14:57:29 +04:00
parent 826f9d4f7e
commit c83018751c
14 changed files with 110 additions and 112 deletions

View File

@ -1291,7 +1291,7 @@ Item *Item::const_charset_converter(THD *thd, CHARSET_INFO *tocs,
uint conv_errors;
Item_string *conv= (func_name ?
new (mem_root)
Item_static_string_func(thd, func_name,
Item_static_string_func(thd, Lex_cstring(func_name),
s, tocs, &conv_errors,
collation.derivation,
collation.repertoire) :
@ -2014,7 +2014,7 @@ Item_name_const::Item_name_const(THD *thd, Item *name_arg, Item *val):
Item::maybe_null= TRUE;
if (name_item->basic_const_item() &&
(name_str= name_item->val_str(&name_buffer))) // Can't have a NULL name
set_name(thd, name_str->ptr(), name_str->length(), name_str->charset());
set_name(thd, name_str->lex_cstring(), name_str->charset());
}
@ -4560,9 +4560,9 @@ Item *Item_param::value_clone_item(THD *thd)
case DECIMAL_RESULT:
return 0; // Should create Item_decimal. See MDEV-11361.
case STRING_RESULT:
return new (mem_root) Item_string(thd, name.str,
value.m_string.c_ptr_quick(),
value.m_string.length(),
return new (mem_root) Item_string(thd, name,
Lex_cstring(value.m_string.c_ptr_quick(),
value.m_string.length()),
value.m_string.charset(),
collation.derivation,
collation.repertoire);
@ -6554,8 +6554,7 @@ int Item_string::save_in_field(Field *field, bool no_conversions)
Item *Item_string::clone_item(THD *thd)
{
return new (thd->mem_root)
Item_string(thd, name.str, str_value.ptr(),
str_value.length(), collation.collation);
Item_string(thd, name, str_value.lex_cstring(), collation.collation);
}

View File

@ -946,6 +946,11 @@ public:
#endif
} /*lint -e1509 */
void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs);
void set_name(THD *thd, const LEX_CSTRING &str,
CHARSET_INFO *cs= system_charset_info)
{
set_name(thd, str.str, str.length, cs);
}
void set_name_no_truncate(THD *thd, const char *str, uint length,
CHARSET_INFO *cs);
void init_make_send_field(Send_field *tmp_field, const Type_handler *h);
@ -4257,7 +4262,7 @@ protected:
const Metadata metadata)
{
fix_from_value(dv, metadata);
set_name(thd, str_value.ptr(), str_value.length(), str_value.charset());
set_name(thd, str_value.lex_cstring(), str_value.charset());
}
protected:
/* Just create an item and do not fill string representation */
@ -4304,21 +4309,21 @@ public:
fix_and_set_name_from_value(thd, dv, Metadata(&str_value, repertoire));
}
// Constructors with an externally provided item name
Item_string(THD *thd, const char *name_par, const char *str, size_t length,
Item_string(THD *thd, const LEX_CSTRING &name_par, const LEX_CSTRING &str,
CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE)
:Item_literal(thd)
{
str_value.set_or_copy_aligned(str, length, cs);
str_value.set_or_copy_aligned(str.str, str.length, cs);
fix_from_value(dv, Metadata(&str_value));
set_name(thd, name_par,safe_strlen(name_par), system_charset_info);
set_name(thd, name_par);
}
Item_string(THD *thd, const char *name_par, const char *str, size_t length,
Item_string(THD *thd, const LEX_CSTRING &name_par, const LEX_CSTRING &str,
CHARSET_INFO *cs, Derivation dv, uint repertoire)
:Item_literal(thd)
{
str_value.set_or_copy_aligned(str, length, cs);
str_value.set_or_copy_aligned(str.str, str.length, cs);
fix_from_value(dv, Metadata(&str_value, repertoire));
set_name(thd, name_par, safe_strlen(name_par), system_charset_info);
set_name(thd, name_par);
}
void print_value(String *to) const
{
@ -4393,13 +4398,13 @@ public:
class Item_string_with_introducer :public Item_string
{
public:
Item_string_with_introducer(THD *thd, const char *str, uint length,
Item_string_with_introducer(THD *thd, const LEX_CSTRING &str,
CHARSET_INFO *cs):
Item_string(thd, str, length, cs)
Item_string(thd, str.str, str.length, cs)
{ }
Item_string_with_introducer(THD *thd, const char *name_arg,
const char *str, uint length, CHARSET_INFO *tocs):
Item_string(thd, name_arg, str, length, tocs)
Item_string_with_introducer(THD *thd, const LEX_CSTRING &name_arg,
const LEX_CSTRING &str, CHARSET_INFO *tocs):
Item_string(thd, name_arg, str, tocs)
{ }
virtual bool is_cs_specified() const
{
@ -4436,14 +4441,14 @@ public:
class Item_static_string_func :public Item_string
{
const char *func_name;
const LEX_CSTRING func_name;
public:
Item_static_string_func(THD *thd, const char *name_par, const char *str,
uint length, CHARSET_INFO *cs,
Item_static_string_func(THD *thd, const LEX_CSTRING &name_par,
const LEX_CSTRING &str, CHARSET_INFO *cs,
Derivation dv= DERIVATION_COERCIBLE):
Item_string(thd, NullS, str, length, cs, dv), func_name(name_par)
Item_string(thd, LEX_CSTRING({NullS,0}), str, cs, dv), func_name(name_par)
{}
Item_static_string_func(THD *thd, const char *name_par,
Item_static_string_func(THD *thd, const LEX_CSTRING &name_par,
const String *str,
CHARSET_INFO *tocs, uint *conv_errors,
Derivation dv, uint repertoire):
@ -4452,7 +4457,7 @@ public:
{}
Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
{
return const_charset_converter(thd, tocs, true, func_name);
return const_charset_converter(thd, tocs, true, func_name.str);
}
virtual inline void print(String *str, enum_query_type query_type)
@ -4465,7 +4470,7 @@ public:
bool check_vcol_func_processor(void *arg)
{ // VCOL_TIME_FUNC because the value is not constant, but does not
// require fix_fields() to be re-run for every statement.
return mark_unsupported_function(func_name, arg, VCOL_TIME_FUNC);
return mark_unsupported_function(func_name.str, arg, VCOL_TIME_FUNC);
}
};
@ -4474,10 +4479,12 @@ public:
class Item_partition_func_safe_string: public Item_string
{
public:
Item_partition_func_safe_string(THD *thd, const char *name_arg, uint length,
CHARSET_INFO *cs= NULL):
Item_string(thd, name_arg, length, cs)
{}
Item_partition_func_safe_string(THD *thd, const LEX_CSTRING &name_arg,
uint length, CHARSET_INFO *cs):
Item_string(thd, name_arg, LEX_CSTRING({0,0}), cs)
{
max_length= length;
}
bool check_vcol_func_processor(void *arg)
{
return mark_unsupported_function("safe_string", arg, VCOL_IMPOSSIBLE);
@ -4489,9 +4496,11 @@ class Item_return_date_time :public Item_partition_func_safe_string
{
enum_field_types date_time_field_type;
public:
Item_return_date_time(THD *thd, const char *name_arg, uint length_arg,
Item_return_date_time(THD *thd, const LEX_CSTRING &name_arg,
enum_field_types field_type_arg, uint dec_arg= 0):
Item_partition_func_safe_string(thd, name_arg, length_arg, &my_charset_bin),
Item_partition_func_safe_string(thd, name_arg,
0/*length is not important*/,
&my_charset_bin),
date_time_field_type(field_type_arg)
{ decimals= dec_arg; }
const Type_handler *type_handler() const
@ -4504,10 +4513,9 @@ public:
class Item_blob :public Item_partition_func_safe_string
{
public:
Item_blob(THD *thd, const char *name_arg, uint length):
Item_partition_func_safe_string(thd, name_arg, (uint) safe_strlen(name_arg),
&my_charset_bin)
{ max_length= length; }
Item_blob(THD *thd, const LEX_CSTRING &name_arg, uint length):
Item_partition_func_safe_string(thd, name_arg, length, &my_charset_bin)
{ }
enum Type type() const { return TYPE_HOLDER; }
const Type_handler *type_handler() const
{
@ -4533,15 +4541,15 @@ public:
class Item_empty_string :public Item_partition_func_safe_string
{
public:
Item_empty_string(THD *thd, const char *header,uint length,
CHARSET_INFO *cs= NULL):
Item_partition_func_safe_string(thd, "", 0,
cs ? cs : &my_charset_utf8_general_ci)
{
name.str= header;
name.length= strlen(name.str);
max_length= length * collation.collation->mbmaxlen;
}
Item_empty_string(THD *thd, const LEX_CSTRING &header, uint length,
CHARSET_INFO *cs= &my_charset_utf8_general_ci)
:Item_partition_func_safe_string(thd, header, length * cs->mbmaxlen, cs)
{ }
Item_empty_string(THD *thd, const char *header, uint length,
CHARSET_INFO *cs= &my_charset_utf8_general_ci)
:Item_partition_func_safe_string(thd, LEX_CSTRING({header, strlen(header)}),
length * cs->mbmaxlen, cs)
{ }
void make_send_field(THD *thd, Send_field *field);
};

View File

@ -6933,9 +6933,9 @@ Item*
Create_func_version::create_builder(THD *thd)
{
thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
return new (thd->mem_root) Item_static_string_func(thd, "version()",
server_version,
(uint) strlen(server_version),
static Lex_cstring name("version()");
return new (thd->mem_root) Item_static_string_func(thd, name,
Lex_cstring(server_version),
system_charset_info,
DERIVATION_SYSCONST);
}

View File

@ -5749,8 +5749,7 @@ find_field_in_view(THD *thd, TABLE_LIST *table_list,
the replacing item.
*/
if (*ref && !(*ref)->is_autogenerated_name)
item->set_name(thd, (*ref)->name.str, (*ref)->name.length,
system_charset_info);
item->set_name(thd, (*ref)->name);
if (register_tree_change)
thd->change_item_tree(ref, item);
else
@ -5841,8 +5840,7 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, si
the replacing item.
*/
if (*ref && !(*ref)->is_autogenerated_name)
item->set_name(thd, (*ref)->name.str, (*ref)->name.length,
system_charset_info);
item->set_name(thd, (*ref)->name);
if (register_tree_change && arena)
thd->restore_active_arena(arena, &backup);

View File

@ -2516,8 +2516,7 @@ THD::make_string_literal_charset(const Lex_string_with_metadata_st &str,
{
if (!str.length && (variables.sql_mode & MODE_EMPTY_STRING_IS_NULL))
return new (mem_root) Item_null(this, 0, cs);
return new (mem_root) Item_string_with_introducer(this,
str.str, (uint)str.length, cs);
return new (mem_root) Item_string_with_introducer(this, str, cs);
}

View File

@ -970,7 +970,7 @@ With_element::rename_columns_of_derived_unit(THD *thd,
/* Rename the columns of the first select in the unit */
while ((item= it++, name= nm++))
{
item->set_name(thd, name->str, (uint) name->length, system_charset_info);
item->set_name(thd, *name);
item->is_autogenerated_name= false;
}

View File

@ -125,9 +125,7 @@ int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table)
NullS, NullS, &field_name);
if (field)
{
field->set_name(thd, field_info->old_name,
(uint) strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
if (add_item_to_list(thd, field))
return 1;
}

View File

@ -8170,7 +8170,6 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
ST_FIELD_INFO *fields_info= schema_table->fields_info;
ST_FIELD_INFO *fields;
CHARSET_INFO *cs= system_charset_info;
MEM_ROOT *mem_root= thd->mem_root;
MY_BITMAP bitmap;
my_bitmap_map *buf;
@ -8192,7 +8191,6 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
for (field_count=0; fields_info->field_name; fields_info++)
{
size_t field_name_length= strlen(fields_info->field_name);
switch (fields_info->field_type) {
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_LONG:
@ -8211,23 +8209,20 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
break;
case MYSQL_TYPE_DATE:
if (!(item=new (mem_root)
Item_return_date_time(thd, fields_info->field_name,
(uint)field_name_length,
Item_return_date_time(thd, fields_info->get_name(),
fields_info->field_type)))
DBUG_RETURN(0);
break;
case MYSQL_TYPE_TIME:
if (!(item=new (mem_root)
Item_return_date_time(thd, fields_info->field_name,
(uint)field_name_length,
Item_return_date_time(thd, fields_info->get_name(),
fields_info->field_type)))
DBUG_RETURN(0);
break;
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATETIME:
if (!(item=new (mem_root)
Item_return_date_time(thd, fields_info->field_name,
(uint)field_name_length,
Item_return_date_time(thd, fields_info->get_name(),
fields_info->field_type,
fields_info->field_length)))
DBUG_RETURN(0);
@ -8261,7 +8256,7 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
item->max_length+= 1;
if (item->decimals > 0)
item->max_length+= 1;
item->set_name(thd, fields_info->field_name, field_name_length, cs);
item->set_name(thd, fields_info->get_name());
break;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
@ -8270,7 +8265,7 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
if (bitmap_is_set(&bitmap, field_count))
{
if (!(item= new (mem_root)
Item_blob(thd, fields_info->field_name,
Item_blob(thd, fields_info->get_name(),
fields_info->field_length)))
{
DBUG_RETURN(0);
@ -8279,12 +8274,11 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
else
{
if (!(item= new (mem_root)
Item_empty_string(thd, "", 0, cs)))
Item_empty_string(thd, "", 0, system_charset_info)))
{
DBUG_RETURN(0);
}
item->set_name(thd, fields_info->field_name,
field_name_length, cs);
item->set_name(thd, fields_info->get_name());
}
break;
default:
@ -8296,12 +8290,12 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
show_field= bitmap_is_set(&bitmap, field_count);
if (!(item= new (mem_root)
Item_empty_string(thd, "",
show_field ? fields_info->field_length : 0, cs)))
show_field ? fields_info->field_length : 0,
system_charset_info)))
{
DBUG_RETURN(0);
}
item->set_name(thd, fields_info->field_name,
field_name_length, cs);
item->set_name(thd, fields_info->get_name());
break;
}
}
@ -8312,7 +8306,7 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
TMP_TABLE_PARAM *tmp_table_param =
(TMP_TABLE_PARAM*) (thd->alloc(sizeof(TMP_TABLE_PARAM)));
tmp_table_param->init();
tmp_table_param->table_charset= cs;
tmp_table_param->table_charset= system_charset_info;
tmp_table_param->field_count= field_count;
tmp_table_param->schema_table= 1;
SELECT_LEX *select_lex= table_list->select_lex;
@ -8357,15 +8351,12 @@ static int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
if (field_info->old_name)
{
LEX_CSTRING field_name= {field_info->field_name,
strlen(field_info->field_name)};
LEX_CSTRING field_name= field_info->get_name();
Item_field *field= new (thd->mem_root)
Item_field(thd, context, NullS, NullS, &field_name);
if (field)
{
field->set_name(thd, field_info->old_name,
strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
if (add_item_to_list(thd, field))
return 1;
}
@ -8386,22 +8377,20 @@ int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
String buffer(tmp,sizeof(tmp), system_charset_info);
LEX_CSTRING field_name= {field_info->field_name,
strlen(field_info->field_name) };
LEX_CSTRING field_name= field_info->get_name();
Item_field *field= new (thd->mem_root) Item_field(thd, context,
NullS, NullS, &field_name);
if (!field || add_item_to_list(thd, field))
return 1;
buffer.length(0);
buffer.append(field_info->old_name);
buffer.append(field_info->get_old_name());
if (lex->wild && lex->wild->ptr())
{
buffer.append(STRING_WITH_LEN(" ("));
buffer.append(lex->wild->ptr());
buffer.append(')');
}
field->set_name(thd, buffer.ptr(), buffer.length(), system_charset_info);
field->set_name(thd, buffer.lex_cstring());
}
return 0;
}
@ -8418,7 +8407,7 @@ int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
strlen(field_info->field_name) };
buffer.length(0);
buffer.append(field_info->old_name);
buffer.append(field_info->get_old_name());
buffer.append(&lex->first_select_lex()->db);
if (lex->wild && lex->wild->ptr())
{
@ -8430,7 +8419,7 @@ int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
NullS, NullS, &field_name);
if (add_item_to_list(thd, field))
return 1;
field->set_name(thd, buffer.ptr(), buffer.length(), system_charset_info);
field->set_name(thd, buffer.lex_cstring());
if (thd->lex->verbose)
{
field_info= &schema_table->fields_info[3];
@ -8440,8 +8429,7 @@ int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
&field_name2);
if (add_item_to_list(thd, field))
return 1;
field->set_name(thd, field_info->old_name, strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
}
return 0;
}
@ -8467,9 +8455,7 @@ int make_columns_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
NullS, NullS, &field_name);
if (field)
{
field->set_name(thd, field_info->old_name,
strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
if (add_item_to_list(thd, field))
return 1;
}
@ -8494,9 +8480,7 @@ int make_character_sets_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
NullS, NullS, &field_name);
if (field)
{
field->set_name(thd, field_info->old_name,
strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
if (add_item_to_list(thd, field))
return 1;
}
@ -8521,9 +8505,7 @@ int make_proc_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
NullS, NullS, &field_name);
if (field)
{
field->set_name(thd, field_info->old_name,
strlen(field_info->old_name),
system_charset_info);
field->set_name(thd, field_info->get_old_name());
if (add_item_to_list(thd, field))
return 1;
}
@ -10118,7 +10100,7 @@ static bool show_create_trigger_impl(THD *thd, Trigger *trigger)
Item_datetime_literal *tmp= (new (mem_root)
Item_datetime_literal(thd, &zero_time, 2));
tmp->set_name(thd, STRING_WITH_LEN("Created"), system_charset_info);
tmp->set_name(thd, Lex_cstring("Created"));
fields.push_back(tmp, mem_root);
if (p->send_result_set_metadata(&fields,

View File

@ -6888,10 +6888,10 @@ Item *Type_handler_string_result::
String *result= item->val_str(&tmp);
if (item->null_value)
return new (thd->mem_root) Item_null(thd, item->name.str);
uint length= result->length();
char *tmp_str= thd->strmake(result->ptr(), length);
return new (thd->mem_root) Item_string(thd, item->name.str,
tmp_str, length, result->charset());
LEX_CSTRING value;
thd->make_lex_string(&value, result->ptr(), result->length());
return new (thd->mem_root) Item_string(thd, item->name, value,
result->charset());
}

View File

@ -556,7 +556,7 @@ bool mysql_create_view(THD *thd, TABLE_LIST *views,
}
while ((item= it++, name= nm++))
{
item->set_name(thd, name->str, (uint) name->length, system_charset_info);
item->set_name(thd, *name);
item->is_autogenerated_name= FALSE;
}
}

View File

@ -9570,7 +9570,7 @@ select_item:
check_column_name($4.str)))
my_yyabort_error((ER_WRONG_COLUMN_NAME, MYF(0), $4.str));
$2->is_autogenerated_name= FALSE;
$2->set_name(thd, $4.str, $4.length, system_charset_info);
$2->set_name(thd, $4);
}
else if (!$2->name.str || $2->name.str == item_empty_name)
{
@ -11186,7 +11186,7 @@ udf_expr:
if ($4.str)
{
$2->is_autogenerated_name= FALSE;
$2->set_name(thd, $4.str, $4.length, system_charset_info);
$2->set_name(thd, $4);
}
/*
A field has to have its proper name in order for name
@ -15123,8 +15123,8 @@ literal:
will include the introducer and the original hex/bin notation.
*/
item_str= new (thd->mem_root)
Item_string_with_introducer(thd, NULL, $2->ptr(), $2->length(),
$1);
Item_string_with_introducer(thd, null_clex_str,
$2->lex_cstring(), $1);
if (unlikely(!item_str ||
!item_str->check_well_formed_result(true)))
MYSQL_YYABORT;

View File

@ -9687,7 +9687,7 @@ select_item:
check_column_name($4.str)))
my_yyabort_error((ER_WRONG_COLUMN_NAME, MYF(0), $4.str));
$2->is_autogenerated_name= FALSE;
$2->set_name(thd, $4.str, $4.length, system_charset_info);
$2->set_name(thd, $4);
}
else if (!$2->name.str || $2->name.str == item_empty_name)
{
@ -11312,7 +11312,7 @@ udf_expr:
if ($4.str)
{
$2->is_autogenerated_name= FALSE;
$2->set_name(thd, $4.str, $4.length, system_charset_info);
$2->set_name(thd, $4);
}
/*
A field has to have its proper name in order for name
@ -15271,8 +15271,8 @@ literal:
will include the introducer and the original hex/bin notation.
*/
item_str= new (thd->mem_root)
Item_string_with_introducer(thd, NULL, $2->ptr(), $2->length(),
$1);
Item_string_with_introducer(thd, null_clex_str,
$2->lex_cstring(), $1);
if (unlikely(!item_str ||
!item_str->check_well_formed_result(true)))
MYSQL_YYABORT;

View File

@ -1703,6 +1703,15 @@ typedef struct st_field_info
@c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
*/
uint open_method;
LEX_CSTRING get_name() const
{
return LEX_CSTRING({field_name, strlen(field_name)});
}
LEX_CSTRING get_old_name() const
{
return LEX_CSTRING({old_name, strlen(old_name)});
}
} ST_FIELD_INFO;

View File

@ -53,6 +53,11 @@ class Lex_cstring : public LEX_CSTRING
str= NULL;
length= 0;
}
Lex_cstring(const char *_str)
{
str= _str;
length= strlen(_str);
}
Lex_cstring(const char *_str, size_t _len)
{
str= _str;