]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-105436: The environment block should end with two null wchar_t values ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 12 Jun 2023 16:49:57 +0000 (09:49 -0700)
committerGitHub <noreply@github.com>
Mon, 12 Jun 2023 16:49:57 +0000 (16:49 +0000)
gh-105436: The environment block should end with two null wchar_t values (GH-105495)
(cherry picked from commit 4f7d3b602d47d61137e82145f601dccfe6f6cd3c)

Co-authored-by: Dora203 <66343334+sku2000@users.noreply.github.com>
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst [new file with mode: 0644]
Modules/_winapi.c

index 846f76ba6d73d3b11cf33f205e3691db48ca4f9e..91162da4f7768891a98d21a96525ece69d40f31a 100644 (file)
@@ -1690,6 +1690,13 @@ class RunFuncTestCase(BaseTestCase):
         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'); "
diff --git a/Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst b/Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst
new file mode 100644 (file)
index 0000000..1e3f298
--- /dev/null
@@ -0,0 +1,2 @@
+Ensure that an empty environment block is terminated by two null characters,
+as is required by Windows.
index f6bb07fd8b06ef31864f8ad2dfe4602dc00ba9f6..5c61d99a837515d817416c561242679ad740cfd4 100644 (file)
@@ -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");
@@ -878,7 +889,8 @@ getenvironment(PyObject* environment)
     *p++ = L'\0';
     assert(p == end);
 
- error:
+cleanup:
+error:
     Py_XDECREF(keys);
     Py_XDECREF(values);
     return buffer;