diff options
author | Monty <monty@mariadb.org> | 2017-04-23 19:39:57 +0300 |
---|---|---|
committer | Monty <monty@mariadb.org> | 2017-04-23 22:35:46 +0300 |
commit | 5a759d31f766087d5e135e1d3d3d987693bc9b88 (patch) | |
tree | 93c7359e8b211e269bfa73e5f595f34b9dca575a /sql/sql_db.cc | |
parent | cba84469eb96481568a9f4ddf3f2989c49c9294c (diff) | |
download | mariadb-git-5a759d31f766087d5e135e1d3d3d987693bc9b88.tar.gz |
Changing field::field_name and Item::name to LEX_CSTRING
Benefits of this patch:
- Removed a lot of calls to strlen(), especially for field_string
- Strings generated by parser are now const strings, less chance of
accidently changing a string
- Removed a lot of calls with LEX_STRING as parameter (changed to pointer)
- More uniform code
- Item::name_length was not kept up to date. Now fixed
- Several bugs found and fixed (Access to null pointers,
access of freed memory, wrong arguments to printf like functions)
- Removed a lot of casts from (const char*) to (char*)
Changes:
- This caused some ABI changes
- lex_string_set now uses LEX_CSTRING
- Some fucntions are now taking const char* instead of char*
- Create_field::change and after changed to LEX_CSTRING
- handler::connect_string, comment and engine_name() changed to LEX_CSTRING
- Checked printf() related calls to find bugs. Found and fixed several
errors in old code.
- A lot of changes from LEX_STRING to LEX_CSTRING, especially related to
parsing and events.
- Some changes from LEX_STRING and LEX_STRING & to LEX_CSTRING*
- Some changes for char* to const char*
- Added printf argument checking for my_snprintf()
- Introduced null_clex_str, star_clex_string, temp_lex_str to simplify
code
- Added item_empty_name and item_used_name to be able to distingush between
items that was given an empty name and items that was not given a name
This is used in sql_yacc.yy to know when to give an item a name.
- select table_name."*' is not anymore same as table_name.*
- removed not used function Item::rename()
- Added comparision of item->name_length before some calls to
my_strcasecmp() to speed up comparison
- Moved Item_sp_variable::make_field() from item.h to item.cc
- Some minimal code changes to avoid copying to const char *
- Fixed wrong error message in wsrep_mysql_parse()
- Fixed wrong code in find_field_in_natural_join() where real_item() was
set when it shouldn't
- ER_ERROR_ON_RENAME was used with extra arguments.
- Removed some (wrong) ER_OUTOFMEMORY, as alloc_root will already
give the error.
TODO:
- Check possible unsafe casts in plugin/auth_examples/qa_auth_interface.c
- Change code to not modify LEX_CSTRING for database name
(as part of lower_case_table_names)
Diffstat (limited to 'sql/sql_db.cc')
-rw-r--r-- | sql/sql_db.cc | 74 |
1 files changed, 39 insertions, 35 deletions
diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 6054c45b1c3..f91b92b1d4f 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -53,16 +53,16 @@ const char *del_exts[]= {".BAK", ".opt", NullS}; static TYPELIB deletable_extentions= {array_elements(del_exts)-1,"del_exts", del_exts, NULL}; -static bool find_db_tables_and_rm_known_files(THD *, MY_DIR *, char *, +static bool find_db_tables_and_rm_known_files(THD *, MY_DIR *, const char *, const char *, TABLE_LIST **); long mysql_rm_arc_files(THD *thd, MY_DIR *dirp, const char *org_path); static my_bool rm_dir_w_symlink(const char *org_path, my_bool send_error); static void mysql_change_db_impl(THD *thd, - LEX_STRING *new_db_name, + LEX_CSTRING *new_db_name, ulong new_db_access, CHARSET_INFO *new_db_charset); -static bool mysql_rm_db_internal(THD *thd, char *db, +static bool mysql_rm_db_internal(THD *thd, const char *db, bool if_exists, bool silent); @@ -122,8 +122,8 @@ uchar* dboptions_get_key(my_dbopt_t *opt, size_t *length, Helper function to write a query to binlog used by mysql_rm_db() */ -static inline int write_to_binlog(THD *thd, char *query, uint q_len, - char *db, uint db_len) +static inline int write_to_binlog(THD *thd, const char *query, uint q_len, + const char *db, uint db_len) { Query_log_event qinfo(thd, query, q_len, FALSE, TRUE, FALSE, 0); qinfo.db= db; @@ -566,7 +566,7 @@ CHARSET_INFO *get_default_db_collation(THD *thd, const char *db_name) */ static int -mysql_create_db_internal(THD *thd, char *db, +mysql_create_db_internal(THD *thd, const char *db, const DDL_options_st &options, Schema_specification_st *create_info, bool silent) @@ -584,7 +584,7 @@ mysql_create_db_internal(THD *thd, char *db, } char db_tmp[SAFE_NAME_LEN]; - char *dbnorm= normalize_db_name(db, db_tmp, sizeof(db_tmp)); + const char *dbnorm= normalize_db_name(db, db_tmp, sizeof(db_tmp)); if (lock_schema_name(thd, dbnorm)) DBUG_RETURN(-1); @@ -764,7 +764,7 @@ exit: } -int mysql_create_db(THD *thd, char *db, +int mysql_create_db(THD *thd, const char *db, const DDL_options_st &options, const Schema_specification_st *create_info) { @@ -805,7 +805,7 @@ bool mysql_alter_db(THD *thd, const char *db, */ static bool -mysql_rm_db_internal(THD *thd,char *db, bool if_exists, bool silent) +mysql_rm_db_internal(THD *thd, const char *db, bool if_exists, bool silent) { ulong deleted_tables= 0; bool error= true, rm_mysql_schema; @@ -818,7 +818,7 @@ mysql_rm_db_internal(THD *thd,char *db, bool if_exists, bool silent) DBUG_ENTER("mysql_rm_db"); char db_tmp[SAFE_NAME_LEN]; - char *dbnorm= normalize_db_name(db, db_tmp, sizeof(db_tmp)); + const char *dbnorm= normalize_db_name(db, db_tmp, sizeof(db_tmp)); if (lock_schema_name(thd, dbnorm)) DBUG_RETURN(true); @@ -884,8 +884,8 @@ mysql_rm_db_internal(THD *thd,char *db, bool if_exists, bool silent) { for (table= tables; table; table= table->next_local) { - LEX_STRING db_name= { table->db, table->db_length }; - LEX_STRING table_name= { table->table_name, table->table_name_length }; + LEX_CSTRING db_name= { table->db, table->db_length }; + LEX_CSTRING table_name= { table->table_name, table->table_name_length }; if (table->open_type == OT_BASE_ONLY || !thd->find_temporary_table(table)) (void) delete_statistics_for_table(thd, &db_name, &table_name); @@ -997,7 +997,8 @@ update_binlog: if (ha_table_exists(thd, tbl->db, tbl->table_name)) continue; - my_snprintf(quoted_name, sizeof(quoted_name), "%`s", tbl->table_name); + my_snprintf(quoted_name, sizeof(quoted_name), quoted_string, + tbl->table_name); tbl_name_len= strlen(quoted_name) + 1; /* +1 for the comma */ if (query_pos + tbl_name_len + 1 >= query_end) { @@ -1048,25 +1049,25 @@ exit: } -bool mysql_rm_db(THD *thd,char *db, bool if_exists) +bool mysql_rm_db(THD *thd, const char *db, bool if_exists) { return mysql_rm_db_internal(thd, db, if_exists, false); } static bool find_db_tables_and_rm_known_files(THD *thd, MY_DIR *dirp, - char *dbname, + const char *dbname, const char *path, TABLE_LIST **tables) { char filePath[FN_REFLEN]; - LEX_STRING db= { dbname, strlen(dbname) }; + LEX_CSTRING db= { dbname, strlen(dbname) }; TABLE_LIST *tot_list=0, **tot_list_next_local, **tot_list_next_global; DBUG_ENTER("find_db_tables_and_rm_known_files"); DBUG_PRINT("enter",("path: %s", path)); /* first, get the list of tables */ - Dynamic_array<LEX_STRING*> files(dirp->number_of_files); + Dynamic_array<LEX_CSTRING*> files(dirp->number_of_files); Discovered_table_list tl(thd, &files); if (ha_discover_table_names(thd, &db, dirp, &tl, true)) DBUG_RETURN(1); @@ -1076,7 +1077,7 @@ static bool find_db_tables_and_rm_known_files(THD *thd, MY_DIR *dirp, for (size_t idx=0; idx < files.elements(); idx++) { - LEX_STRING *table= files.at(idx); + LEX_CSTRING *table= files.at(idx); /* Drop the table nicely */ TABLE_LIST *table_list=(TABLE_LIST*)thd->calloc(sizeof(*table_list)); @@ -1092,7 +1093,7 @@ static bool find_db_tables_and_rm_known_files(THD *thd, MY_DIR *dirp, /* To be able to correctly look up the table in the table cache. */ if (lower_case_table_names) table_list->table_name_length= my_casedn_str(files_charset_info, - table_list->table_name); + (char*) table_list->table_name); table_list->alias= table_list->table_name; // If lower_case_table_names=2 table_list->mdl_request.init(MDL_key::TABLE, table_list->db, @@ -1298,7 +1299,7 @@ err: */ static void mysql_change_db_impl(THD *thd, - LEX_STRING *new_db_name, + LEX_CSTRING *new_db_name, ulong new_db_access, CHARSET_INFO *new_db_charset) { @@ -1330,7 +1331,7 @@ static void mysql_change_db_impl(THD *thd, the previous database name, we should do it explicitly. */ thd->set_db(NULL, 0); - thd->reset_db(new_db_name->str, new_db_name->length); + thd->reset_db(const_cast<char*>(new_db_name->str), new_db_name->length); } /* 2. Update security context. */ @@ -1442,9 +1443,10 @@ static void backup_current_db_name(THD *thd, @retval TRUE Error */ -bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) +bool mysql_change_db(THD *thd, const LEX_CSTRING *new_db_name, + bool force_switch) { - LEX_STRING new_db_file_name; + LEX_CSTRING new_db_file_name; Security_context *sctx= thd->security_ctx; ulong db_access= sctx->db_access; @@ -1507,12 +1509,13 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) It's next to impossible however to get this error when we are called from sp_head::execute(). But let's switch the current database to NULL in this case to be sure. + The cast below ok here as new_db_file_name was just allocated */ - if (check_db_name(&new_db_file_name)) + if (check_db_name((LEX_STRING*) &new_db_file_name)) { my_error(ER_WRONG_DB_NAME, MYF(0), new_db_file_name.str); - my_free(new_db_file_name.str); + my_free(const_cast<char*>(new_db_file_name.str)); if (force_switch) mysql_change_db_impl(thd, NULL, 0, thd->variables.collation_server); @@ -1546,7 +1549,7 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) new_db_file_name.str); general_log_print(thd, COM_INIT_DB, ER_THD(thd, ER_DBACCESS_DENIED_ERROR), sctx->priv_user, sctx->priv_host, new_db_file_name.str); - my_free(new_db_file_name.str); + my_free(const_cast<char*>(new_db_file_name.str)); DBUG_RETURN(TRUE); } #endif @@ -1563,7 +1566,7 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) ER_BAD_DB_ERROR, ER_THD(thd, ER_BAD_DB_ERROR), new_db_file_name.str); - my_free(new_db_file_name.str); + my_free(const_cast<char*>(new_db_file_name.str)); /* Change db to NULL. */ @@ -1577,7 +1580,7 @@ bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch) /* Report an error and free new_db_file_name. */ my_error(ER_BAD_DB_ERROR, MYF(0), new_db_file_name.str); - my_free(new_db_file_name.str); + my_free(const_cast<char*>(new_db_file_name.str)); /* The operation failed. */ @@ -1622,7 +1625,7 @@ done: */ bool mysql_opt_change_db(THD *thd, - const LEX_STRING *new_db_name, + const LEX_CSTRING *new_db_name, LEX_STRING *saved_db_name, bool force_switch, bool *cur_db_changed) @@ -1652,7 +1655,8 @@ bool mysql_opt_change_db(THD *thd, @param old_db 5.0 database name, in #mysql50#name format @return 0 on success, 1 on error */ -bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) + +bool mysql_upgrade_db(THD *thd, LEX_CSTRING *old_db) { int error= 0, change_to_newdb= 0; char path[FN_REFLEN+16]; @@ -1661,7 +1665,7 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) MY_DIR *dirp; TABLE_LIST *table_list; SELECT_LEX *sl= thd->lex->current_select; - LEX_STRING new_db; + LEX_CSTRING new_db; DBUG_ENTER("mysql_upgrade_db"); if ((old_db->length <= MYSQL50_TABLE_NAME_PREFIX_LENGTH) || @@ -1717,7 +1721,7 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) { FILEINFO *file= dirp->dir_entry + idx; char *extension, tname[FN_REFLEN + 1]; - LEX_STRING table_str; + LEX_CSTRING table_str; DBUG_PRINT("info",("Examining: %s", file->name)); /* skiping non-FRM files */ @@ -1731,8 +1735,8 @@ bool mysql_upgrade_db(THD *thd, LEX_STRING *old_db) table_str.length= filename_to_tablename(file->name, tname, sizeof(tname)-1); table_str.str= (char*) thd->memdup(tname, table_str.length + 1); - Table_ident *old_ident= new Table_ident(thd, *old_db, table_str, 0); - Table_ident *new_ident= new Table_ident(thd, new_db, table_str, 0); + Table_ident *old_ident= new Table_ident(thd, old_db, &table_str, 0); + Table_ident *new_ident= new Table_ident(thd, &new_db, &table_str, 0); if (!old_ident || !new_ident || !sl->add_table_to_list(thd, old_ident, NULL, TL_OPTION_UPDATING, TL_IGNORE, @@ -1881,7 +1885,7 @@ bool check_db_dir_existence(const char *db_name) } -char *normalize_db_name(char *db, char *buffer, size_t buffer_size) +const char *normalize_db_name(const char *db, char *buffer, size_t buffer_size) { DBUG_ASSERT(buffer_size > 1); if (!lower_case_table_names) |