Skip to content

[3.11] gh-105436: The environment block should end with two null wchar_t values (GH-105495) #105701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,13 @@ def test_run_with_pathlike_path_and_arguments(self):
res = subprocess.run(args)
self.assertEqual(res.returncode, 57)

@unittest.skipUnless(mswindows, "Maybe test trigger a leak on Ubuntu")
def test_run_with_an_empty_env(self):
# gh-105436: fix subprocess.run(..., env={}) broken on Windows
args = [sys.executable, "-c", 'import sys; sys.exit(57)']
res = subprocess.run(args, env={})
self.assertEqual(res.returncode, 57)

def test_capture_output(self):
cp = self.run_python(("import sys;"
"sys.stdout.write('BDFL'); "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure that an empty environment block is terminated by two null characters,
as is required by Windows.
14 changes: 13 additions & 1 deletion Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,17 @@ getenvironment(PyObject* environment)
}

envsize = PyList_GET_SIZE(keys);

if (envsize == 0) {
// A environment block must be terminated by two null characters --
// one for the last string and one for the block.
buffer = PyMem_Calloc(2, sizeof(wchar_t));
if (!buffer) {
PyErr_NoMemory();
}
goto cleanup;
}

if (PyList_GET_SIZE(values) != envsize) {
PyErr_SetString(PyExc_RuntimeError,
"environment changed size during iteration");
Expand Down Expand Up @@ -878,7 +889,8 @@ getenvironment(PyObject* environment)
*p++ = L'\0';
assert(p == end);

error:
cleanup:
error:
Py_XDECREF(keys);
Py_XDECREF(values);
return buffer;
Expand Down