From d3c6837e4d7b17cbba398600c13cd820adc51430 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 27 Jan 2025 13:07:44 +0100 Subject: [PATCH 1/3] gh-129346: Assert SQLite aggregate context cannot be NULL in the step handler --- Modules/_sqlite/connection.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index fc03e4a085c179..e4734f7604a6cf 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -958,6 +958,7 @@ step_callback(sqlite3_context *context, int argc, sqlite3_value **params) assert(ctx != NULL); aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); + assert(aggregate_instance != NULL); if (*aggregate_instance == NULL) { *aggregate_instance = PyObject_CallNoArgs(ctx->callable); if (!*aggregate_instance) { From 14b45f7d11d1f0d102e8a8885762402eb5220a59 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 27 Jan 2025 14:01:25 +0100 Subject: [PATCH 2/3] Handle memory allocation errors --- Modules/_sqlite/connection.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index e4734f7604a6cf..62598ecc864120 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -958,7 +958,11 @@ step_callback(sqlite3_context *context, int argc, sqlite3_value **params) assert(ctx != NULL); aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); - assert(aggregate_instance != NULL); + if (aggregate_instance == NULL) { + (void)PyErr_NoMemory(); + set_sqlite_error(context, "unable to allocate SQLite aggregate context"); + goto error; + } if (*aggregate_instance == NULL) { *aggregate_instance = PyObject_CallNoArgs(ctx->callable); if (!*aggregate_instance) { From 095f6263893a5d61c0d71e4bfefd5750046f236b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 27 Jan 2025 14:05:30 +0100 Subject: [PATCH 3/3] NEWS --- .../next/Library/2025-01-27-14-05-19.gh-issue-129346.gZRd3g.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-01-27-14-05-19.gh-issue-129346.gZRd3g.rst diff --git a/Misc/NEWS.d/next/Library/2025-01-27-14-05-19.gh-issue-129346.gZRd3g.rst b/Misc/NEWS.d/next/Library/2025-01-27-14-05-19.gh-issue-129346.gZRd3g.rst new file mode 100644 index 00000000000000..b5377277f6c51c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-27-14-05-19.gh-issue-129346.gZRd3g.rst @@ -0,0 +1,2 @@ +In :mod:`sqlite3`, handle out-of-memory when creating user-defined SQL +functions.