Skip to content

Commit 63350df

Browse files
author
Magne Mahre
committed
Bug #33831 mysql_real_connect() connects again if
given an already connected MYSQL handle mysql_real_connect() did not check whether the MYSQL connection handler was already connected and connected again even if so. Now a CR_ALREADY_CONNECTED error is returned.
1 parent 10b43fc commit 63350df

5 files changed

Lines changed: 65 additions & 13 deletions

File tree

include/errmsg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern const char *client_errors[]; /* Error messages */
9797
#define CR_SERVER_LOST_EXTENDED 2055
9898
#define CR_STMT_CLOSED 2056
9999
#define CR_NEW_STMT_METADATA 2057
100-
#define CR_ERROR_LAST /*Copy last error nr:*/ 2057
100+
#define CR_ALREADY_CONNECTED 2058
101+
#define CR_ERROR_LAST /*Copy last error nr:*/ 2058
101102
/* Add error numbers before CR_ERROR_LAST and change it accordingly. */
102103

libmysql/errmsg.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const char *client_errors[]=
8585
"Lost connection to MySQL server at '%s', system error: %d",
8686
"Statement closed indirectly because of a preceeding %s() call",
8787
"The number of columns in the result set differs from the number of bound buffers. You must reset the statement, rebind the result set columns, and execute the statement again",
88+
"This handle is already connected. Use a separate handle for each connection."
8889
""
8990
};
9091

@@ -151,6 +152,7 @@ const char *client_errors[]=
151152
"Lost connection to MySQL server at '%s', system error: %d",
152153
"Statement closed indirectly because of a preceeding %s() call",
153154
"The number of columns in the result set differs from the number of bound buffers. You must reset the statement, rebind the result set columns, and execute the statement again",
155+
"This handle is already connected. Use a separate handle for each connection."
154156
""
155157
};
156158

@@ -215,6 +217,7 @@ const char *client_errors[]=
215217
"Lost connection to MySQL server at '%s', system error: %d",
216218
"Statement closed indirectly because of a preceeding %s() call",
217219
"The number of columns in the result set differs from the number of bound buffers. You must reset the statement, rebind the result set columns, and execute the statement again",
220+
"This handle is already connected. Use a separate handle for each connection."
218221
""
219222
};
220223
#endif

libmysqld/libmysqld.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sys/stat.h>
2929
#include <signal.h>
3030
#include <time.h>
31+
#include <sql_common.h>
3132
#include "client_settings.h"
3233
#ifdef HAVE_PWD_H
3334
#include <pwd.h>
@@ -77,17 +78,6 @@ static my_bool is_NT(void)
7778
}
7879
#endif
7980

80-
/**************************************************************************
81-
** Shut down connection
82-
**************************************************************************/
83-
84-
static void end_server(MYSQL *mysql)
85-
{
86-
DBUG_ENTER("end_server");
87-
free_old_query(mysql);
88-
DBUG_VOID_RETURN;
89-
}
90-
9181

9282
int mysql_init_character_set(MYSQL *mysql);
9383

@@ -104,6 +94,13 @@ mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
10494
db ? db : "(Null)",
10595
user ? user : "(Null)"));
10696

97+
/* Test whether we're already connected */
98+
if (mysql->server_version)
99+
{
100+
set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
101+
DBUG_RETURN(0);
102+
}
103+
107104
if (!host || !host[0])
108105
host= mysql->options.host;
109106

@@ -215,7 +212,7 @@ mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
215212
{
216213
/* Free alloced memory */
217214
my_bool free_me=mysql->free_me;
218-
end_server(mysql);
215+
free_old_query(mysql);
219216
mysql->free_me=0;
220217
mysql_close(mysql);
221218
mysql->free_me=free_me;

sql-common/client.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,13 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
19191919
db ? db : "(Null)",
19201920
user ? user : "(Null)"));
19211921

1922+
/* Test whether we're already connected */
1923+
if (net->vio)
1924+
{
1925+
set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
1926+
DBUG_RETURN(0);
1927+
}
1928+
19221929
/* Don't give sigpipe errors if the client doesn't want them */
19231930
set_sigpipe(mysql);
19241931
mysql->methods= &client_methods;

tests/mysql_client_test.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18026,6 +18026,49 @@ static void test_bug44495()
1802618026
DBUG_VOID_RETURN;
1802718027
}
1802818028

18029+
18030+
/**
18031+
Bug# 33831 mysql_real_connect() should fail if
18032+
given an already connected MYSQL handle.
18033+
*/
18034+
18035+
static void test_bug33831(void)
18036+
{
18037+
MYSQL *l_mysql;
18038+
my_bool error;
18039+
18040+
DBUG_ENTER("test_bug33831");
18041+
18042+
error= 0;
18043+
18044+
if (!(l_mysql= mysql_init(NULL)))
18045+
{
18046+
myerror("mysql_init() failed");
18047+
DIE_UNLESS(0);
18048+
}
18049+
if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
18050+
opt_password, current_db, opt_port,
18051+
opt_unix_socket, 0)))
18052+
{
18053+
myerror("connection failed");
18054+
DIE_UNLESS(0);
18055+
}
18056+
18057+
if (mysql_real_connect(l_mysql, opt_host, opt_user,
18058+
opt_password, current_db, opt_port,
18059+
opt_unix_socket, 0))
18060+
{
18061+
myerror("connection should have failed");
18062+
DIE_UNLESS(0);
18063+
}
18064+
18065+
18066+
mysql_close(l_mysql);
18067+
18068+
DBUG_VOID_RETURN;
18069+
}
18070+
18071+
1802918072
/*
1803018073
Read and parse arguments and MySQL options from my.cnf
1803118074
*/
@@ -18336,6 +18379,7 @@ static struct my_tests_st my_tests[]= {
1833618379
{ "test_wl4166_1", test_wl4166_1 },
1833718380
{ "test_wl4166_2", test_wl4166_2 },
1833818381
{ "test_bug38486", test_bug38486 },
18382+
{ "test_bug33831", test_bug33831 },
1833918383
{ "test_bug40365", test_bug40365 },
1834018384
{ "test_bug43560", test_bug43560 },
1834118385
#ifdef HAVE_QUERY_CACHE

0 commit comments

Comments
 (0)