bpo-31764: Prevent a crash in sqlite3.Cursor.close() in case the Cursor object is uninitialized (GH-3958) (#4303)

(cherry picked from commit edb13ae48c)
This commit is contained in:
Miss Islington (bot) 2017-11-06 16:45:19 -08:00 committed by Victor Stinner
parent 9684cf69e3
commit b0331c94c2
3 changed files with 10 additions and 0 deletions

View file

@ -190,6 +190,9 @@ class RegressionTests(unittest.TestCase):
cur = Cursor(con)
with self.assertRaises(sqlite.ProgrammingError):
cur.execute("select 4+5").fetchall()
with self.assertRaisesRegex(sqlite.ProgrammingError,
r'^Base Cursor\.__init__ not called\.$'):
cur.close()
def CheckStrSubclass(self):
"""

View file

@ -0,0 +1,2 @@
Prevent a crash in ``sqlite3.Cursor.close()`` in case the ``Cursor`` object is
uninitialized. Patch by Oren Milman.

View file

@ -916,6 +916,11 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
{
if (!self->connection) {
PyErr_SetString(pysqlite_ProgrammingError,
"Base Cursor.__init__ not called.");
return NULL;
}
if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
return NULL;
}