Skip to content

Commit 3b50390

Browse files
committed
protect against parmeters with bad values
1 parent f9f5416 commit 3b50390

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,34 @@ def test_executemany_use_after_close(self, params_class):
10691069
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
10701070
cu.executemany("insert into tmp(a) values (?)", params_class(cx))
10711071

1072+
def test_executemany_use_after_close_with___len__(self):
1073+
# Prevent SIGSEGV with a len(parameters) closing the connection.
1074+
# Regression test for https://github.com/python/cpython/issues/143198.
1075+
1076+
class PT(CxWrapper):
1077+
def __init__(self, cx, value):
1078+
super().__init__(cx)
1079+
self.value = value
1080+
1081+
def __getitem__(self, i):
1082+
return self.value
1083+
1084+
def __len__(self):
1085+
self.side_effect()
1086+
return 1
1087+
1088+
class Ps(CxWrapper):
1089+
def __iter__(self):
1090+
return iter([PT(cx, 1), PT(cx, 2), PT(cx, 3)])
1091+
1092+
cx = sqlite.connect(":memory:")
1093+
cx.execute("create table tmp(a number)")
1094+
self.addCleanup(cx.close)
1095+
cu = cx.cursor()
1096+
msg = r"Cannot operate on a closed database\."
1097+
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1098+
cu.executemany("insert into tmp(a) values (?)", Ps(cx))
1099+
10721100
def test_fetch_iter(self):
10731101
# Optional DB-API extension.
10741102
self.cu.execute("delete from test")

Modules/_sqlite/cursor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
941941
}
942942

943943
bind_parameters(state, self->statement, parameters);
944-
if (PyErr_Occurred()) {
944+
if (PyErr_Occurred() || !pysqlite_check_connection(self->connection)) {
945945
goto error;
946946
}
947947

0 commit comments

Comments
 (0)