8000 BUG#28020811: Fix multiple reference leaks in the C extension · mysql/mysql-connector-python@2ffc076 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ffc076

Browse files
committed
BUG#28020811: Fix multiple reference leaks in the C extension
This patch removes some reference leaks/unnecessary incremenents: - Exception attribute names - True, False and None reference counts increases - Removed redundant code and comment on MySQL_connected() Thank you for your contribution. Change-Id: I99cfb9f878299d30f9742fcc7fbe6cb99c271f90
1 parent 1a74a1f commit 2ffc076

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ v8.0.32
1717
- BUG#34556157: Kerberos authorization fails when using SSPI as security interface
1818
- BUG#34499578: MySQLCursor.executemany() fails to correctly identify BULK data loading ops
1919
- BUG#30089671: Fix decoding VARBINARY columns when using a prepared cursor
20+
- BUG#28020811: Fix multiple reference leaks in the C extension
2021

2122
v8.0.31
2223
=======

src/exceptions.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ raise_with_session(MYSQL *conn, PyObject *exc_type)
8484
goto ERR;
8585
}
8686

87-
PyObject_SetAttr(err_object, PyUnicode_FromString("sqlstate"), sqlstate);
88-
PyObject_SetAttr(err_object, PyUnicode_FromString("errno"), error_no);
89-
PyObject_SetAttr(err_object, PyUnicode_FromString("msg"), error_msg);
87+
PyObject_SetAttrString(err_object, "sqlstate", sqlstate);
88+
PyObject_SetAttrString(err_object, "errno", error_no);
89+
PyObject_SetAttrString(err_object, "msg", error_msg);
9090

9191
PyErr_SetObject(exc_type, err_object);
9292
goto CLEANUP;
@@ -144,9 +144,9 @@ raise_with_stmt(MYSQL_STMT *stmt, PyObject *exc_type)
144144
goto ERR;
145145
}
146146

147-
PyObject_SetAttr(err_object, PyUnicode_FromString("sqlstate"), sqlstate);
148-
PyObject_SetAttr(err_object, PyUnicode_FromString("errno"), error_no);
149-
PyObject_SetAttr(err_object, PyUnicode_FromString("msg"), error_msg);
147+
PyObject_SetAttrString(err_object, "sqlstate", sqlstate);
148+
PyObject_SetAttrString(err_object, "errno", error_no);
149+
PyObject_SetAttrString(err_object, "msg", error_msg);
150150

151151
PyErr_SetObject(exc_type, err_object);
152152
goto CLEANUP;
@@ -187,9 +187,9 @@ raise_with_string(PyObject *error_msg, PyObject *exc_type)
187187
if (!err_object) {
188188
goto ERR;
189189
}
190-
PyObject_SetAttr(err_object, PyUnicode_FromString("sqlstate"), Py_None);
191-
PyObject_SetAttr(err_object, PyUnicode_FromString("errno"), error_no);
192-
PyObject_SetAttr(err_object, PyUnicode_FromString("msg"), error_msg);
190+
PyObject_SetAttrString(err_object, "sqlstate", Py_None);
191+
PyObject_SetAttrString(err_object, "errno", error_no);
192+
PyObject_SetAttrString(err_object, "msg", error_msg);
193193

194194
PyErr_SetObject(exc_type, err_object);
195195
goto CLEANUP;

src/mysql_capi.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ MySQL_connected(MySQL *self);
6363
}
6464

6565
#define IS_CONNECTED(cnx) \
66-
if ((PyObject *)MySQL_connected(cnx) == Py_False) { \
66+
if (!cnx->connected) { \
6767
raise_with_session(&cnx->session, MySQLInterfaceError); \
6868
return 0; \
6969
}
@@ -313,7 +313,7 @@ void
313313
MySQL_dealloc(MySQL *self)
314314
{
315315
if (self) {
316-
MySQL_free_result(self);
316+
Py_XDECREF(MySQL_free_result(self));
317317
mysql_close(&self->session);
318318

319319
Py_DECREF(self->charset_name);
@@ -503,7 +503,7 @@ MySQL_free_result(MySQL *self)
503503
Py_END_ALLOW_THREADS
504504
}
505505

506-
MySQL_reset_result(self);
506+
Py_XDECREF(MySQL_reset_result(self));
507507

508508
Py_RETURN_NONE;
509509
}
@@ -536,7 +536,7 @@ MySQL_consume_result(MySQL *self)
536536
Py_END_ALLOW_THREADS
537537
}
538538

539-
MySQL_free_result(self);
539+
Py_XDECREF(MySQL_free_result(self));
540540

541541
Py_RETURN_NONE;
542542
}
@@ -781,8 +781,6 @@ MySQL_st_warning_count(MySQL *self)
781781
782782
Return whether the MySQL instance is connected or not.
783783
784-
This function uses the mysql_ping() C API function.
785-
786784
@param self MySQL instance
787785
788786
@return Boolean Object Py_True or Py_False
@@ -795,7 +793,6 @@ MySQL_connected(MySQL *self)
795793
Py_RETURN_FALSE;
796794
}
797795

798-
self->connected = 1;
799796
Py_RETURN_TRUE;
800797
}
801798

@@ -1280,9 +1277,9 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
12801277
PyObject *err_msg = PyUnicode_FromString("sha256_password requires SSL");
12811278
PyObject *err_obj = NULL;
12821279
err_obj = PyObject_CallFunctionObjArgs(exc_type, err_msg, NULL);
1283-
PyObject_SetAttr(err_obj, PyUnicode_FromString("sqlstate"), Py_None);
1284-
PyObject_SetAttr(err_obj, PyUnicode_FromString("errno"), err_no);
1285-
PyObject_SetAttr(err_obj, PyUnicode_FromString("msg"), err_msg);
1280+
PyObject_SetAttrString(err_obj, "sqlstate", Py_None);
1281+
PyObject_SetAttrString(err_obj, "errno", err_no);
1282+
PyObject_SetAttrString(err_obj, "msg", err_msg);
12861283
PyErr_SetObject(exc_type, err_obj);
12871284
Py_XDECREF(exc_type);
12881285
Py_XDECREF(err_no);
@@ -2185,7 +2182,7 @@ MySQL_query(MySQL *self, PyObject *args, PyObject *kwds)
21852182
}
21862183

21872184
if ((&self->session)->field_count == 0) {
2188-
MySQL_reset_result(self);
2185+
Py_XDECREF(MySQL_reset_result(self));
21892186
self->have_result_set = Py_False;
21902187
Py_RETURN_TRUE;
21912188
}
@@ -2494,7 +2491,7 @@ MySQL_next_result(MySQL *self)
24942491
Py_RETURN_FALSE;
24952492
}
24962493

2497-
MySQL_free_result(self);
2494+
Py_XDECREF(MySQL_free_result(self));
24982495
// We had a result before, we check if we can get next one
24992496
Py_BEGIN_ALLOW_THREADS
25002497
have_more = mysql_next_result(&self->session);
@@ -2505,7 +2502,7 @@ MySQL_next_result(MySQL *self)
25052502
return NULL;
25062503
}
25072504

2508-
MySQL_free_result(self);
2505+
Py_XDECREF(MySQL_free_result(self));
25092506
return MySQL_handle_result(self);
25102507
}
25112508

0 commit comments

Comments
 (0)
0