[Python-checkins] bpo-31764: Prevent a crash in sqlite3.Cursor.close() in case the Cursor object is uninitialized (GH-4333)

Victor Stinner webhook-mailer at python.org
Wed Nov 8 04:57:08 EST 2017


https://github.com/python/cpython/commit/cd66d6d632b4a8190e97f265ee163f11b5ae38e6
commit: cd66d6d632b4a8190e97f265ee163f11b5ae38e6
branch: 2.7
author: Oren Milman <orenmn at gmail.com>
committer: Victor Stinner <victor.stinner at gmail.com>
date: 2017-11-08T01:57:02-08:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2017-11-08-11-02-01.bpo-31764.gtlhKj.rst
M Lib/sqlite3/test/regression.py
M Modules/_sqlite/cursor.c

diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 323b25dbb21..7eeac324d27 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -177,6 +177,9 @@ def __init__(self, con):
             pass
         except:
             self.fail("should have raised ProgrammingError")
+        with self.assertRaisesRegexp(sqlite.ProgrammingError,
+                                     r'^Base Cursor\.__init__ not called\.$'):
+            cur.close()
 
     def CheckConnectionConstructorCallCheck(self):
         """
diff --git a/Misc/NEWS.d/next/Library/2017-11-08-11-02-01.bpo-31764.gtlhKj.rst b/Misc/NEWS.d/next/Library/2017-11-08-11-02-01.bpo-31764.gtlhKj.rst
new file mode 100644
index 00000000000..06af91db2be
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-11-08-11-02-01.bpo-31764.gtlhKj.rst
@@ -0,0 +1,2 @@
+Prevent a crash in ``sqlite3.Cursor.close()`` in case the ``Cursor`` object
+is uninitialized. Patch by Oren Milman.
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 3197e78aaed..b7c2d798da5 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -1014,6 +1014,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;
     }



More information about the Python-checkins mailing list