Skip to content

Commit b66971e

Browse files
friedmethane
authored andcommitted
memory leak in ConnectionObject_Initialize when mysql_real_connect fails (#350)
We can't set open=0 after we mysql_init or dealloc will not cleanup the memory. Also if mysql_init returns NULL we are out of memory and shouldn't set open=1, or we could segfault in dealloc if we didn't seg before that.
1 parent e04c597 commit b66971e

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

MySQLdb/_mysql.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,12 @@ _mysql_ConnectionObject_Initialize(
444444
_stringsuck(cipher, value, ssl);
445445
}
446446

447-
Py_BEGIN_ALLOW_THREADS ;
448447
conn = mysql_init(&(self->connection));
448+
if (!conn) {
449+
PyErr_SetNone(PyExc_MemoryError);
450+
return -1;
451+
}
452+
Py_BEGIN_ALLOW_THREADS ;
449453
self->open = 1;
450454
if (connect_timeout) {
451455
unsigned int timeout = connect_timeout;
@@ -497,7 +501,6 @@ _mysql_ConnectionObject_Initialize(
497501

498502
if (!conn) {
499503
_mysql_Exception(self);
500-
self->open = 0;
501504
return -1;
502505
}
503506

0 commit comments

Comments
 (0)