Skip to content

Commit e214aa1

Browse files
committed
MDEV-18281 COM_RESET_CONNECTION changes the connection encoding
Store original charset during client authentication, and restore it for COM_RESET_CONNECTION
1 parent 14a58ce commit e214aa1

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

client/mysqltest.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5966,6 +5966,7 @@ void do_connect(struct st_command *command)
59665966
int read_timeout= 0;
59675967
int write_timeout= 0;
59685968
int connect_timeout= 0;
5969+
char *csname=0;
59695970
struct st_connection* con_slot;
59705971

59715972
static DYNAMIC_STRING ds_connection_name;
@@ -6077,6 +6078,11 @@ void do_connect(struct st_command *command)
60776078
{
60786079
connect_timeout= atoi(con_options + sizeof("connect_timeout=")-1);
60796080
}
6081+
else if (strncasecmp(con_options, "CHARSET=",
6082+
sizeof("CHARSET=") - 1) == 0)
6083+
{
6084+
csname= strdup(con_options + sizeof("CHARSET=") - 1);
6085+
}
60806086
else
60816087
die("Illegal option to connect: %.*s",
60826088
(int) (end - con_options), con_options);
@@ -6114,7 +6120,7 @@ void do_connect(struct st_command *command)
61146120
mysql_options(con_slot->mysql, MYSQL_OPT_COMPRESS, NullS);
61156121
mysql_options(con_slot->mysql, MYSQL_OPT_LOCAL_INFILE, 0);
61166122
mysql_options(con_slot->mysql, MYSQL_SET_CHARSET_NAME,
6117-
charset_info->csname);
6123+
csname?csname: charset_info->csname);
61186124
if (opt_charsets_dir)
61196125
mysql_options(con_slot->mysql, MYSQL_SET_CHARSET_DIR,
61206126
opt_charsets_dir);
@@ -6225,6 +6231,7 @@ void do_connect(struct st_command *command)
62256231
#ifdef HAVE_SMEM
62266232
dynstr_free(&ds_shm);
62276233
#endif
6234+
free(csname);
62286235
DBUG_VOID_RETURN;
62296236
}
62306237

mysql-test/main/reset_connection.result

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ Com_select 10
55
SHOW local STATUS LIKE 'com_select';
66
Variable_name Value
77
Com_select 0
8+
# Test if charset changes after reset (utf8)
9+
connect utf8_conn,localhost,root,,,,,CHARSET=utf8;
10+
connection utf8_conn;
11+
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
12+
RESULT
13+
OK
14+
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
15+
RESULT
16+
OK
17+
disconnect utf8_conn;
18+
# Test if charset changes after reset (latin1)
19+
connect latin1_conn,localhost,root,,,,,CHARSET=latin1;
20+
connection latin1_conn;
21+
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
22+
RESULT
23+
OK
24+
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
25+
RESULT
26+
OK
27+
disconnect latin1_conn;

mysql-test/main/reset_connection.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ SHOW local STATUS LIKE 'com_select';
2323

2424
SHOW local STATUS LIKE 'com_select';
2525

26+
--echo # Test if charset changes after reset (utf8)
27+
connect(utf8_conn,localhost,root,,,,,CHARSET=utf8);
28+
connection utf8_conn;
29+
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
30+
--reset_connection
31+
SELECT IF(@@character_set_client='utf8','OK', 'FAIL') AS RESULT;
32+
disconnect utf8_conn;
33+
34+
--echo # Test if charset changes after reset (latin1)
35+
connect(latin1_conn,localhost,root,,,,,CHARSET=latin1);
36+
connection latin1_conn;
37+
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
38+
--reset_connection
39+
SELECT IF(@@character_set_client='latin1','OK', 'FAIL') AS RESULT;
40+
disconnect latin1_conn;

sql/sql_class.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier, bool skip_global_sys_var_lock)
847847
prepare_derived_at_open= FALSE;
848848
create_tmp_table_for_derived= FALSE;
849849
save_prep_leaf_list= FALSE;
850+
org_charset= 0;
850851
/* Restore THR_THD */
851852
set_current_thd(old_THR_THD);
852853
inc_thread_count();

sql/sql_class.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,9 @@ class THD :public Statement,
23652365
uint dbug_sentry; // watch out for memory corruption
23662366
#endif
23672367
struct st_my_thread_var *mysys_var;
2368+
2369+
/* Original charset number from the first client packet, or COM_CHANGE_USER*/
2370+
CHARSET_INFO *org_charset;
23682371
private:
23692372
/*
23702373
Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from

sql/sql_connect.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ bool thd_init_client_charset(THD *thd, uint cs_number)
796796
cs->csname);
797797
return true;
798798
}
799+
thd->org_charset= cs;
799800
thd->update_charset(cs,cs,cs);
800801
}
801802
return false;

sql/sql_parse.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,9 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
17151715
thd->status_var.com_other++;
17161716
thd->change_user();
17171717
thd->clear_error(); // if errors from rollback
1718+
/* Restore original charset from client authentication packet.*/
1719+
if(thd->org_charset)
1720+
thd->update_charset(thd->org_charset,thd->org_charset,thd->org_charset);
17181721
my_ok(thd, 0, 0, 0);
17191722
break;
17201723
}

0 commit comments

Comments
 (0)