From c3bcda3dcf34650c329c26cdb7968bdd8e4ae82c Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Wed, 11 Oct 2017 22:21:36 +0300 Subject: [PATCH 1/4] init commit --- Lib/sqlite3/test/regression.py | 1 + .../next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst | 2 ++ Modules/_sqlite/cursor.c | 5 +++++ 3 files changed, 8 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py index 7dd0050528f8fa..74c03975ae2c50 100644 --- a/Lib/sqlite3/test/regression.py +++ b/Lib/sqlite3/test/regression.py @@ -188,6 +188,7 @@ def __init__(self, con): cur = Cursor(con) with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4+5").fetchall() + self.assertRaises(sqlite.ProgrammingError, cur.close) def CheckStrSubclass(self): """ diff --git a/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst new file mode 100644 index 00000000000000..daa7b17edc6f3e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.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 cfe2627aacd5b7..b5b4597c992b20 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -890,6 +890,11 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) { + if (self->connection == NULL) { + PyErr_SetString(pysqlite_ProgrammingError, + "Base Cursor.__init__ not called."); + return NULL; + } if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { return NULL; } From c07d198b2c2144265822b5e3bbd0b0bcc8aece49 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Wed, 11 Oct 2017 22:29:31 +0300 Subject: [PATCH 2/4] improve the NEWS.d item --- .../next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst index daa7b17edc6f3e..99ae4d722e7ee7 100644 --- a/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst +++ b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst @@ -1,2 +1,2 @@ -Prevent a crash in `sqlite3.Cursor.close()` in case the Cursor object is +Prevent a crash in `sqlite3.Cursor.close()` in case the `Cursor` object is uninitialized. Patch by Oren Milman. From 25ff4f30ee5fbf996fe4ca5a5e6c01dbb62a638a Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 12 Oct 2017 19:40:03 +0300 Subject: [PATCH 3/4] improve the test and the NEWS.d item, and make a style change in the C code. --- Lib/sqlite3/test/regression.py | 8 +++++--- .../next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst | 2 +- Modules/_sqlite/cursor.c | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py index 74c03975ae2c50..1adcbb9a011e3c 100644 --- a/Lib/sqlite3/test/regression.py +++ b/Lib/sqlite3/test/regression.py @@ -186,10 +186,12 @@ def __init__(self, con): con = sqlite.connect(":memory:") cur = Cursor(con) - with self.assertRaises(sqlite.ProgrammingError): + with self.assertRaisesRegex(sqlite.ProgrammingError, + r'^Base Cursor\.__init__ not called\.$'): cur.execute("select 4+5").fetchall() - self.assertRaises(sqlite.ProgrammingError, cur.close) - + with self.assertRaisesRegex(sqlite.ProgrammingError, + r'^Base Cursor\.__init__ not called\.$'): + cur.close() def CheckStrSubclass(self): """ The Python 3.0 port of the module didn't cope with values of subclasses of str. diff --git a/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst index 99ae4d722e7ee7..a5a4ce548e0df2 100644 --- a/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst +++ b/Misc/NEWS.d/next/Library/2017-10-11-22-18-04.bpo-31764.EMyIkK.rst @@ -1,2 +1,2 @@ -Prevent a crash in `sqlite3.Cursor.close()` in case the `Cursor` object is +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 b5b4597c992b20..cc2b5f404db63a 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -890,7 +890,7 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) { - if (self->connection == NULL) { + if (!self->connection) { PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called."); return NULL; From 7e0cc4c9ae72002a0c4e1fef705dbd05e80595fe Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Thu, 12 Oct 2017 19:56:43 +0300 Subject: [PATCH 4/4] don't change the old test in CheckCursorConstructorCallCheck --- Lib/sqlite3/test/regression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py index 1adcbb9a011e3c..f83b0987835130 100644 --- a/Lib/sqlite3/test/regression.py +++ b/Lib/sqlite3/test/regression.py @@ -186,12 +186,12 @@ def __init__(self, con): con = sqlite.connect(":memory:") cur = Cursor(con) - with self.assertRaisesRegex(sqlite.ProgrammingError, - r'^Base Cursor\.__init__ not called\.$'): + 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): """ The Python 3.0 port of the module didn't cope with values of subclasses of str.