Skip to content

Commit f98d475

Browse files
[3.11] gh-105375: Improve error handling in the sys extension module (#105611) (#105666)
(cherry picked from commit 41cddc2) In _PySys_AddXOptionWithError() and sys_add_xoption(), bail on first error to prevent exceptions from possibly being overwritten.
1 parent a034493 commit f98d475

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bugs in :mod:`sys` where exceptions could end up being overwritten
2+
because of deferred error handling.

Python/sysmodule.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,15 +2360,21 @@ _PySys_AddXOptionWithError(const wchar_t *s)
23602360
const wchar_t *name_end = wcschr(s, L'=');
23612361
if (!name_end) {
23622362
name = PyUnicode_FromWideChar(s, -1);
2363+
if (name == NULL) {
2364+
goto error;
2365+
}
23632366
value = Py_True;
23642367
Py_INCREF(value);
23652368
}
23662369
else {
23672370
name = PyUnicode_FromWideChar(s, name_end - s);
2371+
if (name == NULL) {
2372+
goto error;
2373+
}
23682374
value = PyUnicode_FromWideChar(name_end + 1, -1);
2369-
}
2370-
if (name == NULL || value == NULL) {
2371-
goto error;
2375+
if (value == NULL) {
2376+
goto error;
2377+
}
23722378
}
23732379
if (PyDict_SetItem(opts, name, value) < 0) {
23742380
goto error;
@@ -3019,15 +3025,21 @@ sys_add_xoption(PyObject *opts, const wchar_t *s)
30193025
const wchar_t *name_end = wcschr(s, L'=');
30203026
if (!name_end) {
30213027
name = PyUnicode_FromWideChar(s, -1);
3028+
if (name == NULL) {
3029+
goto error;
3030+
}
30223031
value = Py_True;
30233032
Py_INCREF(value);
30243033
}
30253034
else {
30263035
name = PyUnicode_FromWideChar(s, name_end - s);
3036+
if (name == NULL) {
3037+
goto error;
3038+
}
30273039
value = PyUnicode_FromWideChar(name_end + 1, -1);
3028-
}
3029-
if (name == NULL || value == NULL) {
3030-
goto error;
3040+
if (value == NULL) {
3041+
goto error;
3042+
}
30313043
}
30323044
if (PyDict_SetItem(opts, name, value) < 0) {
30333045
goto error;

0 commit comments

Comments
 (0)