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

gh-105436: The environment block should end with two null wchar_t values (GH-105495)
(cherry picked from commit 4f7d3b602d)

Co-authored-by: Dora203 <66343334+sku2000@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2023-06-12 09:49:57 -07:00 committed by GitHub
parent f5b63eab9b
commit 261d0b4f0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View 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;