summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-12-10 10:10:56 (GMT)
committerGitHub <noreply@github.com>2018-12-10 10:10:56 (GMT)
commit6fef0f1a8162e755f3b46677265b7cf052d9b83f (patch)
tree4fcfb784d0ad3a488eec00e08188a8ded43f3cd8 /Modules
parent72ff7b4c000f7b8199231a0eb1ca4b119fab40a5 (diff)
downloadcpython-6fef0f1a8162e755f3b46677265b7cf052d9b83f.zip
cpython-6fef0f1a8162e755f3b46677265b7cf052d9b83f.tar.gz
cpython-6fef0f1a8162e755f3b46677265b7cf052d9b83f.tar.bz2
bpo-35445: Do not ignore memory errors when create posix.environ. (GH-11049)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/posixmodule.c64
1 files changed, 27 insertions, 37 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 09d724f..d05724a 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)
+ 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) {
+ Py_DECREF(v);
+ Py_DECREF(k);
+ Py_DECREF(d);
+ return NULL;
+ }
}
Py_DECREF(k);
Py_DECREF(v);
}
-#endif
return d;
}