summaryrefslogtreecommitdiffstats
path: root/Lib/sqlite3/test
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@protonmail.com>2022-05-16 15:39:17 (GMT)
committerGitHub <noreply@github.com>2022-05-16 15:39:17 (GMT)
commit69cf0203ab47692efbc261c028e15e0d7a245c57 (patch)
tree77bee6f7687052409e13b72d19429c5a639eb02c /Lib/sqlite3/test
parent7ccdec3d1d837b910cd4fc5525ecde71a1326202 (diff)
downloadcpython-69cf0203ab47692efbc261c028e15e0d7a245c57.zip
cpython-69cf0203ab47692efbc261c028e15e0d7a245c57.tar.gz
cpython-69cf0203ab47692efbc261c028e15e0d7a245c57.tar.bz2
[3.8] gh-80254: Disallow recursive usage of cursors in sqlite3 converters (#92333)
(cherry picked from commit c908dc5b4798c311981bd7e1f7d92fb623ee448b) Co-authored-by: Sergey Fedoseev <fedoseev.sergey@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/sqlite3/test')
-rw-r--r--Lib/sqlite3/test/regression.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 206ecd7..9ef5a80 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -28,6 +28,9 @@ import weakref
import functools
from test import support
+from unittest.mock import patch
+
+
class RegressionTests(unittest.TestCase):
def setUp(self):
self.con = sqlite.connect(":memory:")
@@ -413,10 +416,50 @@ class RegressionTests(unittest.TestCase):
+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()
+ del self.cur
+ del self.con
+
+ def test_recursive_cursor_init(self):
+ conv = lambda x: self.cur.__init__(self.con)
+ with patch.dict(sqlite.converters, {"INIT": conv}):
+ with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
+ self.cur.execute(f'select x as "x [INIT]", x from test')
+
+ def test_recursive_cursor_close(self):
+ conv = lambda x: self.cur.close()
+ with patch.dict(sqlite.converters, {"CLOSE": conv}):
+ with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
+ self.cur.execute(f'select x as "x [CLOSE]", x from test')
+
+ def test_recursive_cursor_fetch(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')
+ with self.assertRaisesRegex(sqlite.ProgrammingError, self.msg):
+ self.cur.fetchall()
+
+
def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check")
+ recursive_cursor = unittest.makeSuite(RecursiveUseOfCursors)
return unittest.TestSuite((
regression_suite,
+ recursive_cursor,
))
def test():