https://github.com/python/cpython/commit/98d4c21c09030e7ec4dfb4a0e726c14202b6e3c3
commit: 98d4c21c09030e7ec4dfb4a0e726c14202b6e3c3
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-10-21T08:20:14Z
summary:

[3.14] gh-140398: fix memory leaks in `readline` module when `PySys_Audit` 
fails (GH-140400) (#140403)

gh-140398: fix memory leaks in `readline` module when `PySys_Audit` fails 
(GH-140400)
(cherry picked from commit e8e0f411bae1dde634fb152a81e1db1ad1cd2eaa)

Co-authored-by: Shamil <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-10-21-09-20-03.gh-issue-140398.SoABwJ.rst
M Modules/readline.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-21-09-20-03.gh-issue-140398.SoABwJ.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-21-09-20-03.gh-issue-140398.SoABwJ.rst
new file mode 100644
index 00000000000000..481dac7f26dd5e
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-21-09-20-03.gh-issue-140398.SoABwJ.rst
@@ -0,0 +1,4 @@
+Fix memory leaks in :mod:`readline` functions
+:func:`~readline.read_init_file`, :func:`~readline.read_history_file`,
+:func:`~readline.write_history_file`, and
+:func:`~readline.append_history_file` when :c:func:`PySys_Audit` fails.
diff --git a/Modules/readline.c b/Modules/readline.c
index 0dd99dc66c08e9..4da66ca6e7b94c 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -255,6 +255,7 @@ readline_read_init_file_impl(PyObject *module, PyObject 
*filename_obj)
         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
             return NULL;
         if (PySys_Audit("open", "OCi", filename_obj, 'r', 0) < 0) {
+            Py_DECREF(filename_bytes);
             return NULL;
         }
         errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
@@ -298,6 +299,7 @@ readline_read_history_file_impl(PyObject *module, PyObject 
*filename_obj)
         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
             return NULL;
         if (PySys_Audit("open", "OCi", filename_obj, 'r', 0) < 0) {
+            Py_DECREF(filename_bytes);
             return NULL;
         }
         errno = read_history(PyBytes_AS_STRING(filename_bytes));
@@ -343,6 +345,7 @@ readline_write_history_file_impl(PyObject *module, PyObject 
*filename_obj)
             return NULL;
         filename = PyBytes_AS_STRING(filename_bytes);
         if (PySys_Audit("open", "OCi", filename_obj, 'w', 0) < 0) {
+            Py_DECREF(filename_bytes);
             return NULL;
         }
     } else {
@@ -400,6 +403,7 @@ readline_append_history_file_impl(PyObject *module, int 
nelements,
             return NULL;
         filename = PyBytes_AS_STRING(filename_bytes);
         if (PySys_Audit("open", "OCi", filename_obj, 'a', 0) < 0) {
+            Py_DECREF(filename_bytes);
             return NULL;
         }
     } else {

_______________________________________________
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