Skip to content

Commit 19f2c47

Browse files
erlend-aaslandsir-sigurdJelleZijlstra
committed
[3.7] gh-80254: Disallow recursive usage of cursors in sqlite3 converters
(cherry picked from commit c908dc5) Co-authored-by: Sergey Fedoseev <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 387f93c commit 19f2c47

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

Lib/sqlite3/test/regression.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import weakref
2828
from test import support
2929

30+
from unittest.mock import patch
31+
32+
3033
class RegressionTests(unittest.TestCase):
3134
def setUp(self):
3235
self.con = sqlite.connect(":memory:")
@@ -444,11 +447,48 @@ class UnhashableType(type):
444447
self.con.execute('SELECT %s()' % aggr_name)
445448

446449

450+
class RecursiveUseOfCursors(unittest.TestCase):
451+
# GH-80254: sqlite3 should not segfault for recursive use of cursors.
452+
msg = "Recursive use of cursors not allowed"
453+
454+
def setUp(self):
455+
self.con = sqlite.connect(":memory:",
456+
detect_types=sqlite.PARSE_COLNAMES)
457+
self.cur = self.con.cursor()
458+
self.cur.execute("create table test(x foo)")
459+
self.cur.executemany("insert into test(x) values (?)",
460+
[("foo",), ("bar",)])
461+
462+
def tearDown(self):
463+
self.cur.close()
464+
self.con.close()
465+
466+
def test_recursive_cursor_init(self):
467+
conv = lambda x: self.cur.__init__(self.con)
468+
with patch.dict(sqlite.converters, {"INIT": conv}):
469+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
470+
self.cur.execute(f'select x as "x [INIT]", x from test')
471+
472+
def test_recursive_cursor_close(self):
473+
conv = lambda x: self.cur.close()
474+
with patch.dict(sqlite.converters, {"CLOSE": conv}):
475+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
476+
self.cur.execute(f'select x as "x [CLOSE]", x from test')
477+
478+
def test_recursive_cursor_fetch(self):
479+
conv = lambda x, l=[]: self.cur.fetchone() if l else l.append(None)
480+
with patch.dict(sqlite.converters, {"ITER": conv}):
481+
self.cur.execute(f'select x as "x [ITER]", x from test')
482+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
483+
self.cur.fetchall()
484+
485+
447486
def suite():
448487
regression_suite = unittest.makeSuite(RegressionTests, "Check")
449488
return unittest.TestSuite((
450489
regression_suite,
451490
unittest.makeSuite(UnhashableCallbacksTestCase),
491+
unittest.makeSuite(RecursiveUseOfCursors),
452492
))
453493

454494
def test():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :exc:`~sqlite3.ProgrammingError` instead of segfaulting on recursive
2+
usage of cursors in :mod:`sqlite3` converters. Patch by Sergey Fedoseev.

Modules/_sqlite/cursor.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,25 @@
2727

2828
PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
2929

30+
static inline int
31+
check_cursor_locked(pysqlite_Cursor *cur)
32+
{
33+
if (cur->locked) {
34+
PyErr_SetString(pysqlite_ProgrammingError,
35+
"Recursive use of cursors not allowed.");
36+
return 0;
37+
}
38+
return 1;
39+
}
40+
3041
static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
3142

3243
static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
3344
{
45+
if (!check_cursor_locked(self)) {
46+
return -1;
47+
}
48+
3449
pysqlite_Connection* connection;
3550

3651
if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
@@ -376,12 +391,9 @@ static int check_cursor(pysqlite_Cursor* cur)
376391
return 0;
377392
}
378393

379-
if (cur->locked) {
380-
PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
381-
return 0;
382-
}
383-
384-
return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
394+
return (pysqlite_check_thread(cur->connection)
395+
&& pysqlite_check_connection(cur->connection)
396+
&& check_cursor_locked(cur));
385397
}
386398

387399
PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
@@ -802,7 +814,9 @@ PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
802814
}
803815

804816
if (rc == SQLITE_ROW) {
817+
self->locked = 1; // GH-80254: Prevent recursive use of cursors.
805818
self->next_row = _pysqlite_fetch_one_row(self);
819+
self->locked = 0;
806820
if (self->next_row == NULL) {
807821
(void)pysqlite_statement_reset(self->statement);
808822
return NULL;
@@ -905,6 +919,10 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
905919

906920
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
907921
{
922+
if (!check_cursor_locked(self)) {
923+
return NULL;
924+
}
925+
908926
if (!self->connection) {
909927
PyErr_SetString(pysqlite_ProgrammingError,
910928
"Base Cursor.__init__ not called.");

0 commit comments

Comments
 (0)