Skip to content

Commit c49bea8

Browse files
committed
gh-159739: Release resources on interpreter startup error paths
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.
1 parent a071788 commit c49bea8

3 files changed

Lines changed: 5 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leaks during interpreter startup when a path configuration file is
2+
empty or too large, or when an audit hook rejects a command.

‎Modules/getpath.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,11 @@ getpath_readlines_impl(PyObject *module, PyObject *pathobj)
409409
size_t cb = fread(buffer, 1, MAX_FILE, fp);
410410
fclose(fp);
411411
if (!cb) {
412+
PyMem_Free(buffer);
412413
return r;
413414
}
414415
if (cb >= MAX_FILE) {
416+
PyMem_Free(buffer);
415417
Py_DECREF(r);
416418
PyErr_SetString(PyExc_MemoryError,
417419
"cannot read file larger than 32KB during initialization");

‎Modules/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ pymain_run_command(wchar_t *command)
286286
}
287287

288288
if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
289+
Py_DECREF(unicode);
289290
return pymain_exit_err_print();
290291
}
291292

0 commit comments

Comments
 (0)