Skip to content

Commit 9073180

Browse files
bpo-43083: Fix error handling in _sqlite3 (GH-24395)
1 parent d64fd4b commit 9073180

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Modules/_sqlite/connection.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,11 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
17721772
(callable != Py_None) ? callable : NULL,
17731773
(callable != Py_None) ? pysqlite_collation_callback : NULL);
17741774
if (rc != SQLITE_OK) {
1775-
PyDict_DelItem(self->collations, uppercase_name);
1775+
if (callable != Py_None) {
1776+
if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1777+
PyErr_Clear();
1778+
}
1779+
}
17761780
_pysqlite_seterror(self->db, NULL);
17771781
goto finally;
17781782
}

Modules/_sqlite/cursor.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
567567
}
568568

569569
if (!multiple) {
570-
Py_DECREF(self->lastrowid);
571570
Py_BEGIN_ALLOW_THREADS
572571
lastrowid = sqlite3_last_insert_rowid(self->connection->db);
573572
Py_END_ALLOW_THREADS
574-
self->lastrowid = PyLong_FromLongLong(lastrowid);
573+
Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
574+
if (self->lastrowid == NULL) {
575+
goto error;
576+
}
575577
}
576578

577579
if (rc == SQLITE_ROW) {
@@ -842,8 +844,11 @@ pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
842844
}
843845

844846
while ((row = pysqlite_cursor_iternext(self))) {
845-
PyList_Append(list, row);
846-
Py_XDECREF(row);
847+
if (PyList_Append(list, row) < 0) {
848+
Py_DECREF(row);
849+
break;
850+
}
851+
Py_DECREF(row);
847852

848853
if (++counter == maxrows) {
849854
break;
@@ -877,8 +882,11 @@ pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
877882
}
878883

879884
while ((row = pysqlite_cursor_iternext(self))) {
880-
PyList_Append(list, row);
881-
Py_XDECREF(row);
885+
if (PyList_Append(list, row) < 0) {
886+
Py_DECREF(row);
887+
break;
888+
}
889+
Py_DECREF(row);
882890
}
883891

884892
if (PyErr_Occurred()) {

0 commit comments

Comments
 (0)