https://github.com/python/cpython/commit/bbaaebd0c13bb28679e2247353515823c28dccf7
commit: bbaaebd0c13bb28679e2247353515823c28dccf7
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-06-13T14:33:01-07:00
summary:

[3.14] gh-151403: Fix use-after-free when an argv item's __fspath__ mutates 
args (GH-151404) (#151446)

gh-151403: Fix use-after-free when an argv item's __fspath__ mutates args 
(GH-151404)

---------
(cherry picked from commit 6679ac07d881f6e0ce30b7cc28b5671eafa20d9d)

Co-authored-by: tonghuaroot (童话) <[email protected]>
Co-authored-by: tonghuaroot <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-06-12-22-46-31.gh-issue-151403.DalZWh.rst
M Modules/_posixsubprocess.c

diff --git 
a/Misc/NEWS.d/next/Library/2026-06-12-22-46-31.gh-issue-151403.DalZWh.rst 
b/Misc/NEWS.d/next/Library/2026-06-12-22-46-31.gh-issue-151403.DalZWh.rst
new file mode 100644
index 00000000000000..ca779ed684e761
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-06-12-22-46-31.gh-issue-151403.DalZWh.rst
@@ -0,0 +1,3 @@
+Fixed a crash in :class:`subprocess.Popen` (and ``_posixsubprocess.fork_exec``)
+when an ``argv`` item's :meth:`~os.PathLike.__fspath__` concurrently mutates 
the
+``args`` sequence being converted.
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index d2d9b257c7da80..d25ceca5fcf3ca 100644
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -1090,8 +1090,14 @@ subprocess_fork_exec_impl(PyObject *module, PyObject 
*process_args,
                 goto cleanup;
             }
             borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
-            if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
+            /* borrowed_arg is only borrowed; its __fspath__() may run Python
+               that drops fast_args' last reference to it. */
+            Py_INCREF(borrowed_arg);
+            if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) {
+                Py_DECREF(borrowed_arg);
                 goto cleanup;
+            }
+            Py_DECREF(borrowed_arg);
             PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
         }
 

_______________________________________________
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