Skip to content

Commit a16a184

Browse files
author
alexandre.vassalotti
committed
Issue #3153: sqlite leaks on error.
Changed statements of the form Py_DECREF(obj), obj = 0 to Py_CLEAR(obj). git-svn-id: http://svn.python.org/projects/python/trunk@64930 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 72f16e1 commit a16a184

2 files changed

Lines changed: 12 additions & 23 deletions

File tree

Modules/_sqlite/connection.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,19 +1014,16 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
10141014
_pysqlite_seterror(self->db, NULL);
10151015
}
10161016

1017-
Py_DECREF(statement);
1018-
statement = 0;
1017+
Py_CLEAR(statement);
10191018
} else {
10201019
weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
10211020
if (!weakref) {
1022-
Py_DECREF(statement);
1023-
statement = 0;
1021+
Py_CLEAR(statement);
10241022
goto error;
10251023
}
10261024

10271025
if (PyList_Append(self->statements, weakref) != 0) {
1028-
Py_DECREF(weakref);
1029-
statement = 0;
1026+
Py_CLEAR(weakref);
10301027
goto error;
10311028
}
10321029

@@ -1050,15 +1047,13 @@ PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args,
10501047

10511048
method = PyObject_GetAttrString(cursor, "execute");
10521049
if (!method) {
1053-
Py_DECREF(cursor);
1054-
cursor = 0;
1050+
Py_CLEAR(cursor);
10551051
goto error;
10561052
}
10571053

10581054
result = PyObject_CallObject(method, args);
10591055
if (!result) {
1060-
Py_DECREF(cursor);
1061-
cursor = 0;
1056+
Py_CLEAR(cursor);
10621057
}
10631058

10641059
error:
@@ -1081,15 +1076,13 @@ PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* a
10811076

10821077
method = PyObject_GetAttrString(cursor, "executemany");
10831078
if (!method) {
1084-
Py_DECREF(cursor);
1085-
cursor = 0;
1079+
Py_CLEAR(cursor);
10861080
goto error;
10871081
}
10881082

10891083
result = PyObject_CallObject(method, args);
10901084
if (!result) {
1091-
Py_DECREF(cursor);
1092-
cursor = 0;
1085+
Py_CLEAR(cursor);
10931086
}
10941087

10951088
error:
@@ -1112,15 +1105,13 @@ PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject*
11121105

11131106
method = PyObject_GetAttrString(cursor, "executescript");
11141107
if (!method) {
1115-
Py_DECREF(cursor);
1116-
cursor = 0;
1108+
Py_CLEAR(cursor);
11171109
goto error;
11181110
}
11191111

11201112
result = PyObject_CallObject(method, args);
11211113
if (!result) {
1122-
Py_DECREF(cursor);
1123-
cursor = 0;
1114+
Py_CLEAR(cursor);
11241115
}
11251116

11261117
error:

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
545545
}
546546
rc = pysqlite_statement_create(self->statement, self->connection, operation);
547547
if (rc != SQLITE_OK) {
548-
self->statement = 0;
548+
Py_CLEAR(self->statement);
549549
goto error;
550550
}
551551
}
@@ -681,8 +681,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
681681
self->next_row = _pysqlite_fetch_one_row(self);
682682
} else if (rc == SQLITE_DONE && !multiple) {
683683
pysqlite_statement_reset(self->statement);
684-
Py_DECREF(self->statement);
685-
self->statement = 0;
684+
Py_CLEAR(self->statement);
686685
}
687686

688687
switch (statement_type) {
@@ -988,8 +987,7 @@ PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
988987

989988
if (self->statement) {
990989
(void)pysqlite_statement_reset(self->statement);
991-
Py_DECREF(self->statement);
992-
self->statement = 0;
990+
Py_CLEAR(self->statement);
993991
}
994992

995993
Py_INCREF(Py_None);

0 commit comments

Comments
 (0)