Skip to content

Commit 8a833a6

Browse files
bpo-43083: Fix error handling in _sqlite3 (GH-24395)
(cherry picked from commit 9073180) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 995a6c0 commit 8a833a6

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
@@ -1704,7 +1704,11 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
17041704
(callable != Py_None) ? callable : NULL,
17051705
(callable != Py_None) ? pysqlite_collation_callback : NULL);
17061706
if (rc != SQLITE_OK) {
1707-
PyDict_DelItem(self->collations, uppercase_name);
1707+
if (callable != Py_None) {
1708+
if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1709+
PyErr_Clear();
1710+
}
1711+
}
17081712
_pysqlite_seterror(self->db, NULL);
17091713
goto finally;
17101714
}

Modules/_sqlite/cursor.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
569569
}
570570

571571
if (!multiple) {
572-
Py_DECREF(self->lastrowid);
573572
Py_BEGIN_ALLOW_THREADS
574573
lastrowid = sqlite3_last_insert_rowid(self->connection->db);
575574
Py_END_ALLOW_THREADS
576-
self->lastrowid = PyLong_FromLongLong(lastrowid);
575+
Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
576+
if (self->lastrowid == NULL) {
577+
goto error;
578+
}
577579
}
578580

579581
if (rc == SQLITE_ROW) {
@@ -802,8 +804,11 @@ PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObj
802804
}
803805

804806
while ((row = pysqlite_cursor_iternext(self))) {
805-
PyList_Append(list, row);
806-
Py_XDECREF(row);
807+
if (PyList_Append(list, row) < 0) {
808+
Py_DECREF(row);
809+
break;
810+
}
811+
Py_DECREF(row);
807812

808813
if (++counter == maxrows) {
809814
break;
@@ -829,8 +834,11 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
829834
}
830835

831836
while ((row = pysqlite_cursor_iternext(self))) {
832-
PyList_Append(list, row);
833-
Py_XDECREF(row);
837+
if (PyList_Append(list, row) < 0) {
838+
Py_DECREF(row);
839+
break;
840+
}
841+
Py_DECREF(row);
834842
}
835843

836844
if (PyErr_Occurred()) {

0 commit comments

Comments
 (0)