From dc90e24978194fba352dca6f9295024884b5c32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Fri, 10 Feb 2017 12:46:44 +0200 Subject: [PATCH] Define a helper class to allow for saving sql_mode using RAII On construction the Sql_mode_save class stores the current THD's sql_mode. On destruction, the THD's mode is restored. --- sql/sql_acl.cc | 29 ++++++++++------------------- sql/sql_class.h | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index ada5ab1cd28..c2712755584 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -1198,11 +1198,10 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) { TABLE *table; READ_RECORD read_record_info; - bool return_val= TRUE; bool check_no_resolve= specialflag & SPECIAL_NO_RESOLVE; char tmp_name[SAFE_NAME_LEN+1]; int password_length; - sql_mode_t old_sql_mode= thd->variables.sql_mode; + Sql_mode_save old_mode_save(thd); DBUG_ENTER("acl_load"); thd->variables.sql_mode&= ~MODE_PAD_CHAR_TO_FULL_LENGTH; @@ -1214,7 +1213,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) { if (init_read_record(&read_record_info, thd, table, NULL, NULL, 1, 1, FALSE)) - goto end; + DBUG_RETURN(TRUE); table->use_all_columns(); while (!(read_record_info.read_record(&read_record_info))) { @@ -1269,7 +1268,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) if (init_read_record(&read_record_info, thd, table=tables[USER_TABLE].table, NULL, NULL, 1, 1, FALSE)) - goto end; + DBUG_RETURN(TRUE); table->use_all_columns(); username_char_length= MY_MIN(table->field[1]->char_length(), @@ -1280,7 +1279,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) { sql_print_error("Fatal error: mysql.user table is damaged or in " "unsupported 3.20 format."); - goto end; + DBUG_RETURN(TRUE); } DBUG_PRINT("info",("user table fields: %d, password length: %d", @@ -1294,7 +1293,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) mysql_mutex_unlock(&LOCK_global_system_variables); sql_print_error("Fatal error: mysql.user table is in old format, " "but server started with --secure-auth option."); - goto end; + DBUG_RETURN(TRUE); } mysql_user_table_is_in_short_password_format= true; if (global_system_variables.old_passwords) @@ -1527,7 +1526,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) if (init_read_record(&read_record_info, thd, table=tables[DB_TABLE].table, NULL, NULL, 1, 1, FALSE)) - goto end; + DBUG_RETURN(TRUE); table->use_all_columns(); while (!(read_record_info.read_record(&read_record_info))) { @@ -1594,7 +1593,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) { if (init_read_record(&read_record_info, thd, table, NULL, NULL, 1, 1, FALSE)) - goto end; + DBUG_RETURN(TRUE); table->use_all_columns(); while (!(read_record_info.read_record(&read_record_info))) { @@ -1603,10 +1602,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) if (proxy.check_validity(check_no_resolve)) continue; if (push_dynamic(&acl_proxy_users, (uchar*) &proxy)) - { - end_read_record(&read_record_info); - goto end; - } + DBUG_RETURN(TRUE); } my_qsort((uchar*) dynamic_element(&acl_proxy_users, 0, ACL_PROXY_USER*), acl_proxy_users.elements, @@ -1624,7 +1620,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) { if (init_read_record(&read_record_info, thd, table, NULL, NULL, 1, 1, FALSE)) - goto end; + DBUG_RETURN(TRUE); table->use_all_columns(); MEM_ROOT temp_root; @@ -1662,12 +1658,7 @@ static bool acl_load(THD *thd, TABLE_LIST *tables) init_check_host(); initialized=1; - return_val= FALSE; - -end: - end_read_record(&read_record_info); - thd->variables.sql_mode= old_sql_mode; - DBUG_RETURN(return_val); + DBUG_RETURN(FALSE); } diff --git a/sql/sql_class.h b/sql/sql_class.h index 3d9cbc65c22..ffdd63f1f4c 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -5712,6 +5712,22 @@ inline bool binlog_should_compress(ulong len) len >= opt_bin_log_compress_min_len; } + +/** + Save thd sql_mode on instantiation. + On destruction it resets the mode to the previously stored value. +*/ +class Sql_mode_save +{ + public: + Sql_mode_save(THD *thd) : thd(thd), old_mode(thd->variables.sql_mode) {} + ~Sql_mode_save() { thd->variables.sql_mode = old_mode; } + + private: + THD *thd; + sql_mode_t old_mode; // SQL mode saved at construction time. +}; + #endif /* MYSQL_SERVER */ #endif /* SQL_CLASS_INCLUDED */