Skip to content

Commit 3601165

Browse files
orenmnmiss-islington
authored andcommitted
bpo-31764: Prevent a crash in sqlite3.Cursor.close() in case the Cursor object is uninitialized (pythonGH-3958)
(cherry picked from commit edb13ae)
1 parent a6ffec2 commit 3601165

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/sqlite3/test/regression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def __init__(self, con):
188188
cur = Cursor(con)
189189
with self.assertRaises(sqlite.ProgrammingError):
190190
cur.execute("select 4+5").fetchall()
191+
with self.assertRaisesRegex(sqlite.ProgrammingError,
192+
r'^Base Cursor\.__init__ not called\.$'):
193+
cur.close()
191194

192195
def CheckStrSubclass(self):
193196
"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent a crash in ``sqlite3.Cursor.close()`` in case the ``Cursor`` object is
2+
uninitialized. Patch by Oren Milman.

Modules/_sqlite/cursor.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,11 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
917917

918918
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
919919
{
920+
if (!self->connection) {
921+
PyErr_SetString(pysqlite_ProgrammingError,
922+
"Base Cursor.__init__ not called.");
923+
return NULL;
924+
}
920925
if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
921926
return NULL;
922927
}

0 commit comments

Comments
 (0)