mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
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.
This commit is contained in:
@ -332,10 +332,6 @@ public:
|
||||
int4store(Ptr + position,value);
|
||||
}
|
||||
|
||||
void qs_append(const char *str)
|
||||
{
|
||||
qs_append(str, (uint32)strlen(str));
|
||||
}
|
||||
void qs_append(const LEX_CSTRING *ls)
|
||||
{
|
||||
DBUG_ASSERT(ls->length < UINT_MAX32 &&
|
||||
@ -390,9 +386,6 @@ public:
|
||||
init_private_data();
|
||||
(void) real_alloc(length_arg);
|
||||
}
|
||||
explicit Binary_string(const char *str)
|
||||
:Binary_string(str, strlen(str))
|
||||
{ }
|
||||
/*
|
||||
NOTE: If one intend to use the c_ptr() method, the following two
|
||||
contructors need the size of memory for STR to be at least LEN+1 (to make
|
||||
@ -418,7 +411,10 @@ public:
|
||||
alloced= thread_specific= 0;
|
||||
}
|
||||
|
||||
~Binary_string() { free(); }
|
||||
~Binary_string()
|
||||
{
|
||||
free();
|
||||
}
|
||||
|
||||
/* Mark variable thread specific it it's not allocated already */
|
||||
inline void set_thread_specific()
|
||||
@ -500,10 +496,20 @@ public:
|
||||
return old;
|
||||
}
|
||||
|
||||
inline void set_quick(char *str, size_t arg_length)
|
||||
/*
|
||||
This is used to set a new buffer for String.
|
||||
However if the String already has an allocated buffer, it will
|
||||
keep that one.
|
||||
It's not to be used to set the value or length of the string.
|
||||
*/
|
||||
inline void set_buffer_if_not_allocated(char *str, size_t arg_length)
|
||||
{
|
||||
if (!alloced)
|
||||
{
|
||||
/*
|
||||
Following should really be set_str(str, 0), but some code may
|
||||
depend on that the String lenth is same as buffer length.
|
||||
*/
|
||||
Static_binary_string::set(str, arg_length);
|
||||
Alloced_length= (uint32) arg_length;
|
||||
}
|
||||
@ -660,8 +666,9 @@ public:
|
||||
my_free(Ptr);
|
||||
}
|
||||
Alloced_length= extra_alloc= 0;
|
||||
Static_binary_string::set(NULL, 0); // Safety
|
||||
Static_binary_string::set(NULL, 0); // Safety, probably not needed
|
||||
}
|
||||
|
||||
inline bool alloc(size_t arg_length)
|
||||
{
|
||||
/*
|
||||
@ -771,10 +778,6 @@ public:
|
||||
String(size_t length_arg)
|
||||
:Binary_string(length_arg)
|
||||
{ }
|
||||
String(const char *str, CHARSET_INFO *cs)
|
||||
:Charset(cs),
|
||||
Binary_string(str)
|
||||
{ }
|
||||
/*
|
||||
NOTE: If one intend to use the c_ptr() method, the following two
|
||||
contructors need the size of memory for STR to be at least LEN+1 (to make
|
||||
@ -809,9 +812,10 @@ public:
|
||||
set_charset(cs);
|
||||
}
|
||||
bool set_ascii(const char *str, size_t arg_length);
|
||||
inline void set_quick(char *str,size_t arg_length, CHARSET_INFO *cs)
|
||||
inline void set_buffer_if_not_allocated(char *str,size_t arg_length,
|
||||
CHARSET_INFO *cs)
|
||||
{
|
||||
Binary_string::set_quick(str, arg_length);
|
||||
Binary_string::set_buffer_if_not_allocated(str, arg_length);
|
||||
set_charset(cs);
|
||||
}
|
||||
bool set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs);
|
||||
@ -931,8 +935,8 @@ public:
|
||||
bool append_introducer_and_hex(const String *str)
|
||||
{
|
||||
return
|
||||
append(STRING_WITH_LEN("_")) ||
|
||||
append(str->charset()->csname) ||
|
||||
append('_') ||
|
||||
append(str->charset()->csname, strlen(str->charset()->csname)) ||
|
||||
append(STRING_WITH_LEN(" 0x")) ||
|
||||
append_hex(str->ptr(), (uint32) str->length());
|
||||
}
|
||||
@ -946,10 +950,6 @@ public:
|
||||
}
|
||||
|
||||
// Append with optional character set conversion from ASCII (e.g. to UCS2)
|
||||
bool append(const char *s)
|
||||
{
|
||||
return append(s, strlen(s));
|
||||
}
|
||||
bool append(const LEX_STRING *ls)
|
||||
{
|
||||
DBUG_ASSERT(ls->length < UINT_MAX32 &&
|
||||
@ -1015,12 +1015,6 @@ public:
|
||||
{
|
||||
return append_for_single_quote(s->ptr(), s->length());
|
||||
}
|
||||
bool append_for_single_quote(const char *st)
|
||||
{
|
||||
size_t len= strlen(st);
|
||||
DBUG_ASSERT(len < UINT_MAX32);
|
||||
return append_for_single_quote(st, (uint32) len);
|
||||
}
|
||||
|
||||
void swap(String &s)
|
||||
{
|
||||
@ -1083,18 +1077,6 @@ public:
|
||||
BinaryStringBuffer() : Binary_string(buff, buff_sz) { length(0); }
|
||||
};
|
||||
|
||||
|
||||
class String_space: public String
|
||||
{
|
||||
public:
|
||||
String_space(uint n)
|
||||
{
|
||||
if (fill(n, ' '))
|
||||
set("", 0, &my_charset_bin);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static inline bool check_if_only_end_space(CHARSET_INFO *cs,
|
||||
const char *str,
|
||||
const char *end)
|
||||
|
Reference in New Issue
Block a user