From b6ff139aa3457949a5bff10070f5b0e9ac0b43d7 Mon Sep 17 00:00:00 2001 From: Monty Date: Wed, 12 Aug 2020 20:29:55 +0300 Subject: Reduce usage of strlen() Changes: - To detect automatic strlen() I removed the methods in String that uses 'const char *' without a length: - String::append(const char*) - Binary_string(const char *str) - String(const char *str, CHARSET_INFO *cs) - append_for_single_quote(const char *) All usage of append(const char*) is changed to either use String::append(char), String::append(const char*, size_t length) or String::append(LEX_CSTRING) - Added STRING_WITH_LEN() around constant string arguments to String::append() - Added overflow argument to escape_string_for_mysql() and escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow. This was needed as most usage of the above functions never tested the result for -1 and would have given wrong results or crashes in case of overflows. - Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING. Changed all Item_func::func_name()'s to func_name_cstring()'s. The old Item_func_or_sum::func_name() is now an inline function that returns func_name_cstring().str. - Changed Item::mode_name() and Item::func_name_ext() to return LEX_CSTRING. - Changed for some functions the name argument from const char * to to const LEX_CSTRING &: - Item::Item_func_fix_attributes() - Item::check_type_...() - Type_std_attributes::agg_item_collations() - Type_std_attributes::agg_item_set_converter() - Type_std_attributes::agg_arg_charsets...() - Type_handler_hybrid_field_type::aggregate_for_result() - Type_handler_geometry::check_type_geom_or_binary() - Type_handler::Item_func_or_sum_illegal_param() - Predicant_to_list_comparator::add_value_skip_null() - Predicant_to_list_comparator::add_value() - cmp_item_row::prepare_comparators() - cmp_item_row::aggregate_row_elements_for_comparison() - Cursor_ref::print_func() - Removes String_space() as it was only used in one cases and that could be simplified to not use String_space(), thanks to the fixed my_vsnprintf(). - Added some const LEX_CSTRING's for common strings: - NULL_clex_str, DATA_clex_str, INDEX_clex_str. - Changed primary_key_name to a LEX_CSTRING - Renamed String::set_quick() to String::set_buffer_if_not_allocated() to clarify what the function really does. - Rename of protocol function: bool store(const char *from, CHARSET_INFO *cs) to bool store_string_or_null(const char *from, CHARSET_INFO *cs). This was done to both clarify the difference between this 'store' function and also to make it easier to find unoptimal usage of store() calls. - Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*) - Changed some 'const char*' arrays to instead be of type LEX_CSTRING. - class Item_func_units now used LEX_CSTRING for name. Other things: - Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character in the prompt would cause some part of the prompt to be duplicated. - Fixed a lot of instances where the length of the argument to append is known or easily obtain but was not used. - Removed some not needed 'virtual' definition for functions that was inherited from the parent. I added override to these. - Fixed Ordered_key::print() to preallocate needed buffer. Old code could case memory overruns. - Simplified some loops when adding char * to a String with delimiters. --- sql/item_strfunc.cc | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'sql/item_strfunc.cc') diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 186693fb128..fc5b4deb9ac 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -554,7 +554,7 @@ String *Item_func_decode_histogram::val_str(String *str) size_t size= my_snprintf(numbuf, sizeof(numbuf), representation_by_type[type], val - prev); str->append(numbuf, size); - str->append(","); + str->append(','); prev= val; } /* show delta with max */ @@ -2145,7 +2145,7 @@ void Item_func_trim::print(String *str, enum_query_type query_type) Item_func::print(str, query_type); return; } - str->append(Item_func_trim::func_name()); + str->append(Item_func_trim::func_name_cstring()); str->append(func_name_ext()); str->append('('); str->append(mode_name()); @@ -2934,13 +2934,14 @@ String *Item_func_make_set::val_str(String *str) void Item_func_char::print(String *str, enum_query_type query_type) { - str->append(Item_func_char::func_name()); + str->append(Item_func_char::func_name_cstring()); str->append('('); print_args(str, 0, query_type); if (collation.collation != &my_charset_bin) { str->append(STRING_WITH_LEN(" using ")); - str->append(collation.collation->csname); + str->append(collation.collation->csname, + strlen(collation.collation->csname)); } str->append(')'); } @@ -3223,7 +3224,7 @@ bool Item_func_pad::fix_length_and_dec() return TRUE; pad_str.set_charset(collation.collation); pad_str.length(0); - pad_str.append(" ", 1); + pad_str.append(' '); } DBUG_ASSERT(collation.collation->mbmaxlen > 0); @@ -3533,7 +3534,8 @@ void Item_func_conv_charset::print(String *str, enum_query_type query_type) str->append(STRING_WITH_LEN("convert(")); args[0]->print(str, query_type); str->append(STRING_WITH_LEN(" using ")); - str->append(collation.collation->csname); + str->append(collation.collation->csname, + strlen(collation.collation->csname)); str->append(')'); } @@ -3573,7 +3575,7 @@ void Item_func_set_collation::print(String *str, enum_query_type query_type) { args[0]->print_parenthesised(str, query_type, precedence()); str->append(STRING_WITH_LEN(" collate ")); - str->append(m_set_collation->name); + str->append(m_set_collation->name, strlen(m_set_collation->name)); } String *Item_func_charset::val_str(String *str) @@ -3704,7 +3706,7 @@ nl: void Item_func_weight_string::print(String *str, enum_query_type query_type) { - str->append(func_name()); + str->append(func_name_cstring()); str->append('('); args[0]->print(str, query_type); str->append(','); @@ -4631,7 +4633,8 @@ void Item_func_dyncol_create::print_arguments(String *str, if (defs[i].cs) { str->append(STRING_WITH_LEN(" charset ")); - str->append(defs[i].cs->csname); + const char *cs= defs[i].cs->csname; + str->append(cs, strlen(cs)); str->append(' '); } break; -- cgit v1.2.1