From 277356ffa3da111a3c45565a5ae69838dba49ebb Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 14:45:20 -0700 Subject: [PATCH] gh-159739: Release resources on interpreter startup error paths (GH-159752) Release path-file read buffers on early return and the command string when an audit hook rejects execution. Backport to 3.15 and 3.14. (cherry picked from commit 36f7c130df22f512d3b81c73ee20b555ef1cb5ea) Co-authored-by: Jelle Zijlstra Co-authored-by: Jelle Zijlstra --- .../2026-10-19-19-33-35.gh-issue-159739.c25c27.rst | 2 ++ Modules/getpath.c | 2 ++ Modules/main.c | 1 + 3 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst new file mode 100644 index 000000000000000..b48d1e76954193a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst @@ -0,0 +1,2 @@ +Fix memory leaks during interpreter startup when a path configuration file is +empty or too large, or when an audit hook rejects a command. diff --git a/Modules/getpath.c b/Modules/getpath.c index 1e75993480ae363..ada8a27be4c70ff 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -374,9 +374,11 @@ getpath_readlines(PyObject *Py_UNUSED(self), PyObject *args) size_t cb = fread(buffer, 1, MAX_FILE, fp); fclose(fp); if (!cb) { + PyMem_Free(buffer); return r; } if (cb >= MAX_FILE) { + PyMem_Free(buffer); Py_DECREF(r); PyErr_SetString(PyExc_MemoryError, "cannot read file larger than 32KB during initialization"); diff --git a/Modules/main.c b/Modules/main.c index 7d30935cd9e356d..9977ad5f2d22ecd 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -253,6 +253,7 @@ pymain_run_command(wchar_t *command) } if (PySys_Audit("cpython.run_command", "O", unicode) < 0) { + Py_DECREF(unicode); return pymain_exit_err_print(); }