From 6fef0f1a8162e755f3b46677265b7cf052d9b83f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 10 Dec 2018 12:10:56 +0200 Subject: [PATCH] bpo-35445: Do not ignore memory errors when create posix.environ. (GH-11049) --- .../2018-12-09-14-35-49.bpo-35445.LjvtsC.rst | 1 + Modules/posixmodule.c | 66 ++++++++----------- 2 files changed, 29 insertions(+), 38 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-09-14-35-49.bpo-35445.LjvtsC.rst diff --git a/Misc/NEWS.d/next/Library/2018-12-09-14-35-49.bpo-35445.LjvtsC.rst b/Misc/NEWS.d/next/Library/2018-12-09-14-35-49.bpo-35445.LjvtsC.rst new file mode 100644 index 00000000000..c0ba2b10e1e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-09-14-35-49.bpo-35445.LjvtsC.rst @@ -0,0 +1 @@ +Memory errors during creating posix.environ no longer ignored. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 09d724f2f4b..d05724a50d1 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -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) - return d; - /* This part ignores errors */ - for (e = _wenviron; *e != NULL; e++) { - PyObject *k; - PyObject *v; - 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); - } + e = _wenviron; #else - if (environ == NULL) + e = environ; +#endif + if (e == NULL) return d; - /* This part ignores errors */ - for (e = environ; *e != NULL; e++) { + for (; *e != NULL; e++) { PyObject *k; PyObject *v; +#ifdef MS_WINDOWS + const wchar_t *p = wcschr(*e, L'='); +#else 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) { + Py_DECREF(v); + Py_DECREF(k); + Py_DECREF(d); + return NULL; + } } Py_DECREF(k); Py_DECREF(v); } -#endif return d; }