Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_sqlite3/test_userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,29 @@ def test_agg_keyword_args(self):
'takes exactly 3 positional arguments'):
self.con.create_aggregate("test", 1, aggregate_class=AggrText)

def test_aggr_close_conn_in_step(self):
"""Connection.close() in an aggregate step callback must not crash."""
Comment thread
raminfp marked this conversation as resolved.
Outdated
con = sqlite.connect(":memory:", autocommit=True)
cur = con.cursor()
cur.execute("CREATE TABLE t(x INTEGER)")
for i in range(50):
cur.execute("INSERT INTO t VALUES (?)", (i,))

class CloseConnAgg:
def __init__(self):
self.total = 0

def step(self, value):
self.total += value
con.close()

def finalize(self):
return self.total

con.create_aggregate("agg_close", 1, CloseConnAgg)
with self.assertRaises(sqlite.ProgrammingError):
con.execute("SELECT agg_close(x) FROM t")


class AuthorizerTests(unittest.TestCase):
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed a crash in the :mod:`sqlite3` module when
:meth:`~sqlite3.Connection.close` is called on the connection during an
aggregate callback (e.g., in the ``step`` method). The interpreter now raises
:exc:`~sqlite3.ProgrammingError` instead of crashing with a segmentation
fault.

Comment thread
raminfp marked this conversation as resolved.
Outdated
7 changes: 6 additions & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,11 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}

rc = stmt_step(self->statement->st);
if (self->connection->db == NULL) {
PyErr_SetString(state->ProgrammingError,
"Cannot operate on a closed database.");
Comment thread
raminfp marked this conversation as resolved.
Outdated
goto error;
}
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
if (PyErr_Occurred()) {
/* there was an error that occurred in a user-defined callback */
Expand Down Expand Up @@ -967,7 +972,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
Py_XDECREF(parameters);
}

if (!multiple) {
if (!multiple && self->connection->db) {
Comment thread
raminfp marked this conversation as resolved.
Outdated
sqlite_int64 lastrowid;

Py_BEGIN_ALLOW_THREADS
Expand Down
Loading