Fix accessing buffer of decrefed string

fixes #78
This commit is contained in:
INADA Naoki
2016-05-12 00:29:12 +09:00
parent 9657ee06d5
commit 012e91a03a

View File

@ -531,6 +531,8 @@ _mysql_ConnectionObject_Initialize(
#if HAVE_OPENSSL #if HAVE_OPENSSL
char *key = NULL, *cert = NULL, *ca = NULL, char *key = NULL, *cert = NULL, *ca = NULL,
*capath = NULL, *cipher = NULL; *capath = NULL, *cipher = NULL;
PyObject *ssl_keepref[5] = {};
int n_ssl_keepref = 0;
#endif #endif
char *host = NULL, *user = NULL, *passwd = NULL, char *host = NULL, *user = NULL, *passwd = NULL,
*db = NULL, *unix_socket = NULL; *db = NULL, *unix_socket = NULL;
@ -586,11 +588,11 @@ _mysql_ConnectionObject_Initialize(
#ifdef IS_PY3K #ifdef IS_PY3K
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\ #define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
if(t){d=PyUnicode_AsUTF8(t);Py_DECREF(t);}\ if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
PyErr_Clear();} PyErr_Clear();}
#else #else
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\ #define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
if(t){d=PyString_AsString(t);Py_DECREF(t);}\ if(t){d=PyString_AsString(t);ssl_keepref[n_ssl_keepref++]=t;}\
PyErr_Clear();} PyErr_Clear();}
#endif #endif
@ -645,9 +647,9 @@ _mysql_ConnectionObject_Initialize(
mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile); mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile);
#if HAVE_OPENSSL #if HAVE_OPENSSL
if (ssl) if (ssl) {
mysql_ssl_set(&(self->connection), mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher);
key, cert, ca, capath, cipher); }
#endif #endif
conn = mysql_real_connect(&(self->connection), host, user, passwd, db, conn = mysql_real_connect(&(self->connection), host, user, passwd, db,
@ -655,6 +657,16 @@ _mysql_ConnectionObject_Initialize(
Py_END_ALLOW_THREADS ; Py_END_ALLOW_THREADS ;
#if HAVE_OPENSSL
if (ssl) {
int i;
for (i=0; i<n_ssl_keepref; i++) {
Py_DECREF(ssl_keepref[i]);
ssl_keepref[i] = NULL;
}
}
#endif
if (!conn) { if (!conn) {
_mysql_Exception(self); _mysql_Exception(self);
return -1; return -1;