From b66971ee36be96b772ae7fdec79ccc1611376f3c Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Mon, 25 Mar 2019 14:46:32 -0700 Subject: [PATCH] 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. --- MySQLdb/_mysql.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 3c84627..f12231b 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -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; }