Skip to content

Commit

Permalink
MDEV-18281 COM_RESET_CONNECTION changes the connection encoding
Browse files Browse the repository at this point in the history
Store original charset during client authentication, and restore it for
COM_RESET_CONNECTION
  • Loading branch information
vaintroub committed Feb 2, 2019
1 parent 14a58ce commit e214aa1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion client/mysqltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5966,6 +5966,7 @@ void do_connect(struct st_command *command)
int read_timeout= 0;
int write_timeout= 0;
int connect_timeout= 0;
char *csname=0;
struct st_connection* con_slot;

static DYNAMIC_STRING ds_connection_name;
Expand Down Expand Up @@ -6077,6 +6078,11 @@ void do_connect(struct st_command *command)
{
connect_timeout= atoi(con_options + sizeof("connect_timeout=")-1);
}
else if (strncasecmp(con_options, "CHARSET=",
sizeof("CHARSET=") - 1) == 0)
{
csname= strdup(con_options + sizeof("CHARSET=") - 1);
}
else
die("Illegal option to connect: %.*s",
(int) (end - con_options), con_options);
Expand Down Expand Up @@ -6114,7 +6120,7 @@ void do_connect(struct st_command *command)
mysql_options(con_slot->mysql, MYSQL_OPT_COMPRESS, NullS);
mysql_options(con_slot->mysql, MYSQL_OPT_LOCAL_INFILE, 0);
mysql_options(con_slot->mysql, MYSQL_SET_CHARSET_NAME,
charset_info->csname);
csname?csname: charset_info->csname);
if (opt_charsets_dir)
mysql_options(con_slot->mysql, MYSQL_SET_CHARSET_DIR,
opt_charsets_dir);
Expand Down Expand Up @@ -6225,6 +6231,7 @@ void do_connect(struct st_command *command)
#ifdef HAVE_SMEM
dynstr_free(&ds_shm);
#endif
free(csname);
DBUG_VOID_RETURN;
}

Expand Down
20 changes: 20 additions & 0 deletions mysql-test/main/reset_connection.result
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ Com_select 10
SHOW local STATUS LIKE 'com_select';
Variable_name Value
Com_select 0
# Test if charset changes after reset (utf8)
connect utf8_conn,localhost,root,,,,,CHARSET=utf8;
connection utf8_conn;
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
RESULT
OK
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
RESULT
OK
disconnect utf8_conn;
# Test if charset changes after reset (latin1)
connect latin1_conn,localhost,root,,,,,CHARSET=latin1;
connection latin1_conn;
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
RESULT
OK
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
RESULT
OK
disconnect latin1_conn;
15 changes: 15 additions & 0 deletions mysql-test/main/reset_connection.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ SHOW local STATUS LIKE 'com_select';

SHOW local STATUS LIKE 'com_select';

--echo # Test if charset changes after reset (utf8)
connect(utf8_conn,localhost,root,,,,,CHARSET=utf8);
connection utf8_conn;
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
--reset_connection
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
disconnect utf8_conn;

--echo # Test if charset changes after reset (latin1)
connect(latin1_conn,localhost,root,,,,,CHARSET=latin1);
connection latin1_conn;
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
--reset_connection
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
disconnect latin1_conn;
1 change: 1 addition & 0 deletions sql/sql_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier, bool skip_global_sys_var_lock)
prepare_derived_at_open= FALSE;
create_tmp_table_for_derived= FALSE;
save_prep_leaf_list= FALSE;
org_charset= 0;
/* Restore THR_THD */
set_current_thd(old_THR_THD);
inc_thread_count();
Expand Down
3 changes: 3 additions & 0 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -2365,6 +2365,9 @@ class THD :public Statement,
uint dbug_sentry; // watch out for memory corruption
#endif
struct st_my_thread_var *mysys_var;

/* Original charset number from the first client packet, or COM_CHANGE_USER*/
CHARSET_INFO *org_charset;
private:
/*
Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
Expand Down
1 change: 1 addition & 0 deletions sql/sql_connect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ bool thd_init_client_charset(THD *thd, uint cs_number)
cs->csname);
return true;
}
thd->org_charset= cs;
thd->update_charset(cs,cs,cs);
}
return false;
Expand Down
3 changes: 3 additions & 0 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,9 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
thd->status_var.com_other++;
thd->change_user();
thd->clear_error(); // if errors from rollback
/* Restore original charset from client authentication packet.*/
if(thd->org_charset)
thd->update_charset(thd->org_charset,thd->org_charset,thd->org_charset);
my_ok(thd, 0, 0, 0);
break;
}
Expand Down

0 comments on commit e214aa1

Please sign in to comment.