Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5b9786

Browse files
erlend-aaslandsir-sigurdJelleZijlstra
committed
[3.9] 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 187cb95 commit a5b9786

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

Lib/sqlite3/test/regression.py

+41
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import functools
2929
from test import support
3030

31+
from unittest.mock import patch
32+
33+
3134
class RegressionTests(unittest.TestCase):
3235
def setUp(self):
3336
self.con = sqlite.connect(":memory:")
@@ -413,10 +416,48 @@ def log(self, *args):
413416

414417

415418

419+
class RecursiveUseOfCursors(unittest.TestCase):
420+
# GH-80254: sqlite3 should not segfault for recursive use of cursors.
421+
msg = "Recursive use of cursors not allowed"
422+
423+
def setUp(self):
424+
self.con = sqlite.connect(":memory:",
425+
detect_types=sqlite.PARSE_COLNAMES)
426+
self.cur = self.con.cursor()
427+
self.cur.execute("create table test(x foo)")
428+
self.cur.executemany("insert into test(x) values (?)",
429+
[("foo",), ("bar",)])
430+
431+
def tearDown(self):
432+
self.cur.close()
433+
self.con.close()
434+
435+
def test_recursive_cursor_init(self):
436+
conv = lambda x: self.cur.__init__(self.con)
437+
with patch.dict(sqlite.converters, {"INIT": conv}):
438+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
439+
self.cur.execute(f'select x as "x [INIT]", x from test')
440+
441+
def test_recursive_cursor_close(self):
442+
conv = lambda x: self.cur.close()
443+
with patch.dict(sqlite.converters, {"CLOSE": conv}):
444+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
445+
self.cur.execute(f'select x as "x [CLOSE]", x from test')
446+
447+
def test_recursive_cursor_fetch(self):
448+
conv = lambda x, l=[]: self.cur.fetchone() if l else l.append(None)
449+
with patch.dict(sqlite.converters, {"ITER": conv}):
450+
self.cur.execute(f'select x as "x [ITER]", x from test')
451+
with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
452+
self.cur.fetchall()
453+
454+
416455
def suite():
417456
regression_suite = unittest.makeSuite(RegressionTests, "Check")
457+
recursive_cursor = unittest.makeSuite(RecursiveUseOfCursors)
418458
return unittest.TestSuite((
419459
regression_suite,
460+
recursive_cursor,
420461
))
421462

422463
def test():
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

+24-6
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))
@@ -357,12 +372,9 @@ static int check_cursor(pysqlite_Cursor* cur)
357372
return 0;
358373
}
359374

360-
if (cur->locked) {
361-
PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
362-
return 0;
363-
}
364-
365-
return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
375+
return (pysqlite_check_thread(cur->connection)
376+
&& pysqlite_check_connection(cur->connection)
377+
&& check_cursor_locked(cur));
366378
}
367379

368380
static PyObject *
@@ -762,7 +774,9 @@ PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
762774
}
763775

764776
if (rc == SQLITE_ROW) {
777+
self->locked = 1; // GH-80254: Prevent recursive use of cursors.
765778
self->next_row = _pysqlite_fetch_one_row(self);
779+
self->locked = 0;
766780
if (self->next_row == NULL) {
767781
(void)pysqlite_statement_reset(self->statement);
768782
return NULL;
@@ -857,6 +871,10 @@ PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
857871

858872
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
859873
{
874+
if (!check_cursor_locked(self)) {
875+
return NULL;
876+
}
877+
860878
if (!self->connection) {
861879
PyErr_SetString(pysqlite_ProgrammingError,
862880
"Base Cursor.__init__ not called.");

0 commit comments

Comments
 (0)