From 5278fbfcf7edd99606ac324a328a15d08e9684ed Mon Sep 17 00:00:00 2001 From: An Long Date: Sat, 29 Aug 2026 18:53:37 +0900 Subject: [PATCH] gh-156555: Fix crash in sqlite3.Row() with uninitialised Cursor --- Lib/test/test_sqlite3/test_factory.py | 6 ++++++ .../Library/2026-08-29-18-52-43.gh-issue-156555.nNV61R.rst | 3 +++ Modules/_sqlite/row.c | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-29-18-52-43.gh-issue-156555.nNV61R.rst diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index 2dd42921d31ddd5..2679ff8ace58b6e 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -280,6 +280,12 @@ class FakeCursor(str): self.assertRaises(TypeError, self.con.cursor, FakeCursor) self.assertRaises(TypeError, sqlite.Row, FakeCursor(), ()) + def test_uninitialised_cursor(self): + cur = sqlite.Cursor.__new__(sqlite.Cursor) + with self.assertRaisesRegex(sqlite.ProgrammingError, + "Base Cursor.__init__ not called"): + sqlite.Row(cur, ()) + class TextFactoryTests(MemoryDatabaseMixin, unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2026-08-29-18-52-43.gh-issue-156555.nNV61R.rst b/Misc/NEWS.d/next/Library/2026-08-29-18-52-43.gh-issue-156555.nNV61R.rst new file mode 100644 index 000000000000000..8bbf2583338d7d7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-29-18-52-43.gh-issue-156555.nNV61R.rst @@ -0,0 +1,3 @@ +Fix a crash in :class:`sqlite3.Row` when it is given a +:class:`sqlite3.Cursor` whose ``__init__()`` was not called; it now raises +:exc:`sqlite3.ProgrammingError` instead. diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 8646f591ef5169f..29967671eac3723 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -88,6 +88,13 @@ pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor, assert(type != NULL && type->tp_alloc != NULL); + if (!cursor->initialized) { + pysqlite_state *state = pysqlite_get_state_by_type(type); + PyErr_SetString(state->ProgrammingError, + "Base Cursor.__init__ not called."); + return NULL; + } + self = (pysqlite_Row *) type->tp_alloc(type, 0); if (self == NULL) return NULL;