https://github.com/python/cpython/commit/d3e976f4f79469a25f7c0039def025c3315c2b59
commit: d3e976f4f79469a25f7c0039def025c3315c2b59
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-10-20T19:29:38Z
summary:

[3.14] gh-159739: Release resources on interpreter startup error paths 
(GH-159752) (#159794)

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 <[email protected]>
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 1e75993480ae36..ada8a27be4c70f 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 f9df068e6dcea1..d2c3b84f3e12cc 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -252,6 +252,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