From 61a7710d3532d2c607fb8476db51694c1bf6f65c Mon Sep 17 00:00:00 2001 From: stu2000 <1907109855@qq.com> Date: Thu, 8 Jun 2023 18:58:47 +0800 Subject: [PATCH 1/9] Fixes 105436 A Unicode environment block is terminated by four zero bytes --- Lib/test/test_subprocess.py | 6 ++++++ Modules/_winapi.c | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 51ba423a0f1c92..48aba0f3153015 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1692,6 +1692,12 @@ def test_run_with_pathlike_path_and_arguments(self): res = subprocess.run(args) self.assertEqual(res.returncode, 57) + def test_run_with_an_empty_rnv(self): + path = FakePath(sys.executable) + args = [path, '-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/Modules/_winapi.c b/Modules/_winapi.c index af13014bf201b0..2d7ca1d7f9e24a 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -796,6 +796,17 @@ getenvironment(PyObject* environment) } envsize = PyList_GET_SIZE(keys); + + /* A Unicode environment block is terminated by four zero bytes: + two for the last string, two more to terminate the block. */ + if (envsize == 0) { + buffer = PyMem_NEW(wchar_t, 2); + p = buffer; + *p++ = L'\0'; + *p++ = L'\0'; + goto error; + } + if (PyList_GET_SIZE(values) != envsize) { PyErr_SetString(PyExc_RuntimeError, "environment changed size during iteration"); From fba488ecb6b462d693d864c7c0c7cb50c169e28d Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Thu, 8 Jun 2023 19:27:07 +0800 Subject: [PATCH 2/9] he environment block should end with two zero-valued wchar_t values --- Lib/test/test_subprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 48aba0f3153015..6b95e3bcb93118 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1697,7 +1697,7 @@ def test_run_with_an_empty_rnv(self): args = [path, '-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'); " From b0eed2dd6bf54f7b37ce16e6e1b23265270583a8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:30:18 +0000 Subject: [PATCH 3/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst 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 index 00000000000000..045082be17d99f --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst @@ -0,0 +1 @@ +the environment block should end with two zero-valued wchar_t values From 65b12252f66a358c5084025f7c26fbac2f9ffd2f Mon Sep 17 00:00:00 2001 From: stu2000 <1907109855@qq.com> Date: Sat, 10 Jun 2023 00:05:04 +0800 Subject: [PATCH 4/9] A environment block must be terminated by two null characters one for the last string and one for the block. --- Lib/test/test_subprocess.py | 5 ++--- Modules/_winapi.c | 17 +++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 48aba0f3153015..1ab1e46d73b2e6 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1692,9 +1692,8 @@ def test_run_with_pathlike_path_and_arguments(self): res = subprocess.run(args) self.assertEqual(res.returncode, 57) - def test_run_with_an_empty_rnv(self): - path = FakePath(sys.executable) - args = [path, '-c', 'import sys; sys.exit(57)'] + def test_run_with_an_empty_env(self): + args = [sys.executable, "-c", 'import sys; sys.exit(57)'] res = subprocess.run(args, env={}) self.assertEqual(res.returncode, 57) diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 2d7ca1d7f9e24a..a7e6bb582fc64d 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -797,14 +797,14 @@ getenvironment(PyObject* environment) envsize = PyList_GET_SIZE(keys); - /* A Unicode environment block is terminated by four zero bytes: - two for the last string, two more to terminate the block. */ if (envsize == 0) { - buffer = PyMem_NEW(wchar_t, 2); - p = buffer; - *p++ = L'\0'; - *p++ = L'\0'; - goto error; + // 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) { @@ -880,7 +880,8 @@ getenvironment(PyObject* environment) *p++ = L'\0'; assert(p == end); - error: +cleanup: +error: Py_XDECREF(keys); Py_XDECREF(values); return buffer; From c626ffcd43826efdd1c6a51d206ec757b366dd57 Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Sat, 10 Jun 2023 01:05:17 +0800 Subject: [PATCH 5/9] Update Misc/NEWS.d/next/Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst Co-authored-by: Eryk Sun --- .../Windows/2023-06-08-11-30-17.gh-issue-105436.1qlDxw.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 045082be17d99f..1e3f298096cdd6 100644 --- 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 @@ -1 +1,2 @@ -the environment block should end with two zero-valued wchar_t values +Ensure that an empty environment block is terminated by two null characters, +as is required by Windows. From 63500cd0f61fa7e4b8e233ea2b76ec3e9d2eeedb Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Sun, 11 Jun 2023 19:57:29 +0800 Subject: [PATCH 6/9] Update test_subprocess.py --- Lib/test/test_subprocess.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index b6586911d4c517..19237c9b6b0eee 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1694,7 +1694,9 @@ def test_run_with_pathlike_path_and_arguments(self): def test_run_with_an_empty_env(self): args = [sys.executable, "-c", 'import sys; sys.exit(57)'] + env = {} res = subprocess.run(args, env={}) + del env self.assertEqual(res.returncode, 57) def test_capture_output(self): From 9e1c241d2bd00f99d2902aab828310dacc85c575 Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Sun, 11 Jun 2023 21:45:18 +0800 Subject: [PATCH 7/9] Update test_subprocess.py --- Lib/test/test_subprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 19237c9b6b0eee..b5543a1fa5bd2a 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1695,7 +1695,7 @@ def test_run_with_pathlike_path_and_arguments(self): def test_run_with_an_empty_env(self): args = [sys.executable, "-c", 'import sys; sys.exit(57)'] env = {} - res = subprocess.run(args, env={}) + res = subprocess.run(args, env=env) del env self.assertEqual(res.returncode, 57) From 77f65abc1f27e0efb302902fc5f6d8e2ae4a4501 Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Mon, 12 Jun 2023 22:51:30 +0800 Subject: [PATCH 8/9] Update test_subprocess.py --- Lib/test/test_subprocess.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index b5543a1fa5bd2a..7cf606f8a72398 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1692,11 +1692,11 @@ 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)'] - env = {} - res = subprocess.run(args, env=env) - del env + res = subprocess.run(args, env={}) self.assertEqual(res.returncode, 57) def test_capture_output(self): From e1d991fbcd1d374d9f46779787d3fd33239abcab Mon Sep 17 00:00:00 2001 From: Dora203 <66343334+sku2000@users.noreply.github.com> Date: Mon, 12 Jun 2023 23:00:04 +0800 Subject: [PATCH 9/9] Update test_subprocess.py --- Lib/test/test_subprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 7cf606f8a72398..3d4fffbb8e794a 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1694,7 +1694,7 @@ def test_run_with_pathlike_path_and_arguments(self): @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 + # 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)