Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Memory errors during creating posix.environ no longer ignored.
64 changes: 27 additions & 37 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1356,62 +1356,52 @@ convertenviron(void)
/* _wenviron must be initialized in this way if the program is started
through main() instead of wmain(). */
_wgetenv(L"");
if (_wenviron == NULL)
e = _wenviron;
#else
e = environ;
#endif
if (e == NULL)
return d;
/* This part ignores errors */
for (e = _wenviron; *e != NULL; e++) {
for (; *e != NULL; e++) {
PyObject *k;
PyObject *v;
#ifdef MS_WINDOWS
const wchar_t *p = wcschr(*e, L'=');
if (p == NULL)
continue;
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
if (k == NULL) {
PyErr_Clear();
continue;
}
v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
if (v == NULL) {
PyErr_Clear();
Py_DECREF(k);
continue;
}
if (PyDict_GetItem(d, k) == NULL) {
if (PyDict_SetItem(d, k, v) != 0)
PyErr_Clear();
}
Py_DECREF(k);
Py_DECREF(v);
}
#else
if (environ == NULL)
return d;
/* This part ignores errors */
for (e = environ; *e != NULL; e++) {
PyObject *k;
PyObject *v;
const char *p = strchr(*e, '=');
#endif
if (p == NULL)
continue;
#ifdef MS_WINDOWS
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
#else
k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
#endif
if (k == NULL) {
PyErr_Clear();
continue;
Py_DECREF(d);
return NULL;
}
#ifdef MS_WINDOWS
v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
#else
v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
#endif
if (v == NULL) {
PyErr_Clear();
Py_DECREF(k);
continue;
Py_DECREF(d);
return NULL;
}
if (PyDict_GetItem(d, k) == NULL) {
if (PyDict_SetItem(d, k, v) != 0)
PyErr_Clear();
if (PyDict_GetItemWithError(d, k) == NULL) {
if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two ifs could be merged.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the resulting condition will be too complex and will need more parentheses and line breaks in any case. I do not think it will look better.

if ((PyDict_GetItemWithError(d, k) == NULL) &&
    (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0))
{

Py_DECREF(v);
Py_DECREF(k);
Py_DECREF(d);
return NULL;
}
}
Py_DECREF(k);
Py_DECREF(v);
}
#endif
return d;
}

Expand Down