Skip to content

Commit 0182d07

Browse files
committed
check null in normalize_environment
1 parent 4de761b commit 0182d07

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

Lib/test/test_subprocess.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ def test_win32_duplicate_envs(self):
788788
newenv = os.environ.copy()
789789
newenv["fruit"] = "lemon"
790790
newenv["FRUIT"] = "orange"
791+
newenv["frUit"] = "banana"
791792
with subprocess.Popen(["CMD", "/c", "SET", "fruit"],
792793
stdout=subprocess.PIPE,
793794
env=newenv) as p:

Modules/_winapi.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -808,18 +808,46 @@ normalize_environment(PyObject* environment) {
808808

809809
result = PyDict_New();
810810

811-
for (int i = 0; i<PyList_GET_SIZE(keys); i++) {
812-
if (i < 1) {
811+
for (int i=0; i<PyList_GET_SIZE(keys); i++) {
812+
PyObject *key = PyList_GET_ITEM(keys, i);
813+
PyObject* value = PyObject_GetItem(environment, key);
814+
815+
if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
816+
PyErr_SetString(PyExc_TypeError,
817+
"environment can only contain strings");
818+
Py_DECREF(result);
819+
result = NULL;
820+
goto error;
821+
}
822+
if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
823+
PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
824+
{
825+
PyErr_SetString(PyExc_ValueError, "embedded null character");
826+
Py_DECREF(result);
827+
result = NULL;
828+
goto error;
829+
}
830+
/* Search from index 1 because on Windows starting '=' is allowed for
831+
defining hidden environment variables. */
832+
if (PyUnicode_GET_LENGTH(key) == 0 ||
833+
PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
834+
{
835+
PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
836+
Py_DECREF(result);
837+
result = NULL;
838+
goto error;
839+
}
840+
841+
if (i == 0) {
813842
continue;
814843
}
815-
PyObject *key = PyList_GET_ITEM(keys, i);
844+
816845
wchar_t *key_string = PyUnicode_AsWideCharString(key, NULL);
817846
wchar_t *prev_key_string = PyUnicode_AsWideCharString(PyList_GET_ITEM(keys, i-1), NULL);
818847
if (CompareStringOrdinal(prev_key_string, -1, key_string, -1, TRUE) == CSTR_EQUAL) {
819848
continue;
820849
}
821-
PyObject* value = PyDict_GetItem(environment, key);
822-
PyDict_SetItem(result, key, value);
850+
PyObject_SetItem(result, key, value);
823851
}
824852

825853
error:
@@ -848,7 +876,7 @@ getenvironment(PyObject* env)
848876

849877
environment = normalize_environment(env);
850878
if (environment == NULL) {
851-
goto error;
879+
return NULL;
852880
}
853881

854882
keys = PyMapping_Keys(environment);
@@ -873,26 +901,6 @@ getenvironment(PyObject* env)
873901
PyObject* value = PyList_GET_ITEM(values, i);
874902
Py_ssize_t size;
875903

876-
if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
877-
PyErr_SetString(PyExc_TypeError,
878-
"environment can only contain strings");
879-
goto error;
880-
}
881-
if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
882-
PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
883-
{
884-
PyErr_SetString(PyExc_ValueError, "embedded null character");
885-
goto error;
886-
}
887-
/* Search from index 1 because on Windows starting '=' is allowed for
888-
defining hidden environment variables. */
889-
if (PyUnicode_GET_LENGTH(key) == 0 ||
890-
PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
891-
{
892-
PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
893-
goto error;
894-
}
895-
896904
size = PyUnicode_AsWideChar(key, NULL, 0);
897905
assert(size > 1);
898906
if (totalsize > PY_SSIZE_T_MAX - size) {

0 commit comments

Comments
 (0)