Skip to content

Commit edb13ae

Browse files
orenmnvstinner
authored andcommitted
bpo-31764: Prevent a crash in sqlite3.Cursor.close() in case the Cursor object is uninitialized (#3958)
1 parent e56ab74 commit edb13ae

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
@@ -190,6 +190,9 @@ def __init__(self, con):
190190
cur = Cursor(con)
191191
with self.assertRaises(sqlite.ProgrammingError):
192192
cur.execute("select 4+5").fetchall()
193+
with self.assertRaisesRegex(sqlite.ProgrammingError,
194+
r'^Base Cursor\.__init__ not called\.$'):
195+
cur.close()
193196

194197
def CheckStrSubclass(self):
195198
"""
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
@@ -889,6 +889,11 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
889889

890890
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
891891
{
892+
if (!self->connection) {
893+
PyErr_SetString(pysqlite_ProgrammingError,
894+
"Base Cursor.__init__ not called.");
895+
return NULL;
896+
}
892897
if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
893898
return NULL;
894899
}

0 commit comments

Comments
 (0)