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.
This commit is contained in:
Jason Fried
2019-03-25 14:46:32 -07:00
committed by Inada Naoki
parent e04c5972f5
commit b66971ee36

View File

@ -444,8 +444,12 @@ _mysql_ConnectionObject_Initialize(
_stringsuck(cipher, value, ssl);
}
Py_BEGIN_ALLOW_THREADS ;
conn = mysql_init(&(self->connection));
if (!conn) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
Py_BEGIN_ALLOW_THREADS ;
self->open = 1;
if (connect_timeout) {
unsigned int timeout = connect_timeout;
@ -497,7 +501,6 @@ _mysql_ConnectionObject_Initialize(
if (!conn) {
_mysql_Exception(self);
self->open = 0;
return -1;
}