gh-105436: The environment block should end with two null wchar_t values (GH-105495)

This commit is contained in:
Dora203 2023-06-13 00:14:55 +08:00 committed by GitHub
parent 2b90796be6
commit 4f7d3b602d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View file

@ -796,6 +796,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");
@ -869,7 +880,8 @@ getenvironment(PyObject* environment)
*p++ = L'\0';
assert(p == end);
error:
cleanup:
error:
Py_XDECREF(keys);
Py_XDECREF(values);
return buffer;