https://github.com/python/cpython/commit/36f7c130df22f512d3b81c73ee20b555ef1cb5ea
commit: 36f7c130df22f512d3b81c73ee20b555ef1cb5ea
branch: main
author: Jelle Zijlstra <[email protected]>
committer: gpshead <[email protected]>
date: 2026-10-19T21:45:20Z
summary:

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.

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst
M Modules/getpath.c
M Modules/main.c

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 00000000000000..b48d1e76954193
--- /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 48047de0c12af2..10964af12819d2 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -409,9 +409,11 @@ getpath_readlines_impl(PyObject *module, PyObject *pathobj)
     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 44a025d500e35e..d2587e9fffdad2 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -286,6 +286,7 @@ pymain_run_command(wchar_t *command)
     }
 
     if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
+        Py_DECREF(unicode);
         return pymain_exit_err_print();
     }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to