diff options
author | Erlend Egeberg Aasland <erlend.aasland@protonmail.com> | 2022-05-03 23:03:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 23:03:06 (GMT) |
commit | f629dcfe835e349433e4c5099381d668e8fe69c8 (patch) | |
tree | 8e33d2ac3c2f55a5404591ce116494a016248639 /Lib/test/test_sqlite3 | |
parent | 836b17c9c3ea313e400e58a75f52b63f96e498bb (diff) | |
download | cpython-f629dcfe835e349433e4c5099381d668e8fe69c8.zip cpython-f629dcfe835e349433e4c5099381d668e8fe69c8.tar.gz cpython-f629dcfe835e349433e4c5099381d668e8fe69c8.tar.bz2 |
gh-80254: Disallow recursive usage of cursors in `sqlite3` converters (#29054)
Co-authored-by: Sergey Fedoseev <fedoseev.sergey@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_sqlite3')
-rw-r--r-- | Lib/test/test_sqlite3/test_regression.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_sqlite3/test_regression.py b/Lib/test/test_sqlite3/test_regression.py index aebea59..19bb84b 100644 --- a/Lib/test/test_sqlite3/test_regression.py +++ b/Lib/test/test_sqlite3/test_regression.py @@ -27,6 +27,7 @@ import weakref import functools from test import support +from unittest.mock import patch from test.test_sqlite3.test_dbapi import memory_database, managed_connect, cx_limit @@ -469,5 +470,43 @@ class RegressionTests(unittest.TestCase): self.assertEqual(steps, values) +class RecursiveUseOfCursors(unittest.TestCase): + # GH-80254: sqlite3 should not segfault for recursive use of cursors. + msg = "Recursive use of cursors not allowed" + + def setUp(self): + self.con = sqlite.connect(":memory:", + detect_types=sqlite.PARSE_COLNAMES) + self.cur = self.con.cursor() + self.cur.execute("create table test(x foo)") + self.cur.executemany("insert into test(x) values (?)", + [("foo",), ("bar",)]) + + def tearDown(self): + self.cur.close() + self.con.close() + + def test_recursive_cursor_init(self): + conv = lambda x: self.cur.__init__(self.con) + with patch.dict(sqlite.converters, {"INIT": conv}): + self.cur.execute(f'select x as "x [INIT]", x from test') + self.assertRaisesRegex(sqlite.ProgrammingError, self.msg, + self.cur.fetchall) + + def test_recursive_cursor_close(self): + conv = lambda x: self.cur.close() + with patch.dict(sqlite.converters, {"CLOSE": conv}): + self.cur.execute(f'select x as "x [CLOSE]", x from test') + self.assertRaisesRegex(sqlite.ProgrammingError, self.msg, + self.cur.fetchall) + + def test_recursive_cursor_iter(self): + conv = lambda x, l=[]: self.cur.fetchone() if l else l.append(None) + with patch.dict(sqlite.converters, {"ITER": conv}): + self.cur.execute(f'select x as "x [ITER]", x from test') + self.assertRaisesRegex(sqlite.ProgrammingError, self.msg, + self.cur.fetchall) + + if __name__ == "__main__": unittest.main() |