diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-30-59.gh-issue-105375.eewafp.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-30-59.gh-issue-105375.eewafp.rst new file mode 100644 index 00000000000000..e000f98828a211 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-30-59.gh-issue-105375.eewafp.rst @@ -0,0 +1,2 @@ +Fix a bug in the :mod:`posix` module where an exception could be +overwritten. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index a01662d868e545..7c367b9d2e9818 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -5691,7 +5691,7 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) { Py_ssize_t i, pos, envc; PyObject *keys=NULL, *vals=NULL; - PyObject *key, *val, *key2, *val2, *keyval; + PyObject *key2, *val2, *keyval; EXECV_CHAR **envlist; i = PyMapping_Size(env); @@ -5716,10 +5716,14 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) } for (pos = 0; pos < i; pos++) { - key = PyList_GetItem(keys, pos); - val = PyList_GetItem(vals, pos); - if (!key || !val) + PyObject *key = PyList_GetItem(keys, pos); // Borrowed ref. + if (key == NULL) { goto error; + } + PyObject *val = PyList_GetItem(vals, pos); // Borrowed ref. + if (val == NULL) { + goto error; + } #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) if (!PyUnicode_FSDecoder(key, &key2))