Skip to content

Commit 6fef0f1

Browse files
bpo-35445: Do not ignore memory errors when create posix.environ. (pythonGH-11049)
1 parent 72ff7b4 commit 6fef0f1

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Memory errors during creating posix.environ no longer ignored.

Modules/posixmodule.c

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,62 +1356,52 @@ convertenviron(void)
13561356
/* _wenviron must be initialized in this way if the program is started
13571357
through main() instead of wmain(). */
13581358
_wgetenv(L"");
1359-
if (_wenviron == NULL)
1359+
e = _wenviron;
1360+
#else
1361+
e = environ;
1362+
#endif
1363+
if (e == NULL)
13601364
return d;
1361-
/* This part ignores errors */
1362-
for (e = _wenviron; *e != NULL; e++) {
1365+
for (; *e != NULL; e++) {
13631366
PyObject *k;
13641367
PyObject *v;
1368+
#ifdef MS_WINDOWS
13651369
const wchar_t *p = wcschr(*e, L'=');
1366-
if (p == NULL)
1367-
continue;
1368-
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1369-
if (k == NULL) {
1370-
PyErr_Clear();
1371-
continue;
1372-
}
1373-
v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1374-
if (v == NULL) {
1375-
PyErr_Clear();
1376-
Py_DECREF(k);
1377-
continue;
1378-
}
1379-
if (PyDict_GetItem(d, k) == NULL) {
1380-
if (PyDict_SetItem(d, k, v) != 0)
1381-
PyErr_Clear();
1382-
}
1383-
Py_DECREF(k);
1384-
Py_DECREF(v);
1385-
}
13861370
#else
1387-
if (environ == NULL)
1388-
return d;
1389-
/* This part ignores errors */
1390-
for (e = environ; *e != NULL; e++) {
1391-
PyObject *k;
1392-
PyObject *v;
13931371
const char *p = strchr(*e, '=');
1372+
#endif
13941373
if (p == NULL)
13951374
continue;
1375+
#ifdef MS_WINDOWS
1376+
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1377+
#else
13961378
k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
1379+
#endif
13971380
if (k == NULL) {
1398-
PyErr_Clear();
1399-
continue;
1381+
Py_DECREF(d);
1382+
return NULL;
14001383
}
1384+
#ifdef MS_WINDOWS
1385+
v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1386+
#else
14011387
v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
1388+
#endif
14021389
if (v == NULL) {
1403-
PyErr_Clear();
14041390
Py_DECREF(k);
1405-
continue;
1391+
Py_DECREF(d);
1392+
return NULL;
14061393
}
1407-
if (PyDict_GetItem(d, k) == NULL) {
1408-
if (PyDict_SetItem(d, k, v) != 0)
1409-
PyErr_Clear();
1394+
if (PyDict_GetItemWithError(d, k) == NULL) {
1395+
if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1396+
Py_DECREF(v);
1397+
Py_DECREF(k);
1398+
Py_DECREF(d);
1399+
return NULL;
1400+
}
14101401
}
14111402
Py_DECREF(k);
14121403
Py_DECREF(v);
14131404
}
1414-
#endif
14151405
return d;
14161406
}
14171407

0 commit comments

Comments
 (0)