Skip to content

Commit a1034b5

Browse files
[3.12] gh-105375: Improve error handling in the sys extension module (GH-105611) (#105665)
In _PySys_AddXOptionWithError() and sys_add_xoption(), bail on first error to prevent exceptions from possibly being overwritten. (cherry picked from commit 41cddc2) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 82ac2be commit a1034b5

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
@@ -2718,14 +2718,20 @@ _PySys_AddXOptionWithError(const wchar_t *s)
27182718
const wchar_t *name_end = wcschr(s, L'=');
27192719
if (!name_end) {
27202720
name = PyUnicode_FromWideChar(s, -1);
2721+
if (name == NULL) {
2722+
goto error;
2723+
}
27212724
value = Py_NewRef(Py_True);
27222725
}
27232726
else {
27242727
name = PyUnicode_FromWideChar(s, name_end - s);
2728+
if (name == NULL) {
2729+
goto error;
2730+
}
27252731
value = PyUnicode_FromWideChar(name_end + 1, -1);
2726-
}
2727-
if (name == NULL || value == NULL) {
2728-
goto error;
2732+
if (value == NULL) {
2733+
goto error;
2734+
}
27292735
}
27302736
if (PyDict_SetItem(opts, name, value) < 0) {
27312737
goto error;
@@ -3370,14 +3376,20 @@ sys_add_xoption(PyObject *opts, const wchar_t *s)
33703376
const wchar_t *name_end = wcschr(s, L'=');
33713377
if (!name_end) {
33723378
name = PyUnicode_FromWideChar(s, -1);
3379+
if (name == NULL) {
3380+
goto error;
3381+
}
33733382
value = Py_NewRef(Py_True);
33743383
}
33753384
else {
33763385
name = PyUnicode_FromWideChar(s, name_end - s);
3386+
if (name == NULL) {
3387+
goto error;
3388+
}
33773389
value = PyUnicode_FromWideChar(name_end + 1, -1);
3378-
}
3379-
if (name == NULL || value == NULL) {
3380-
goto error;
3390+
if (value == NULL) {
3391+
goto error;
3392+
}
33813393
}
33823394
if (PyDict_SetItem(opts, name, value) < 0) {
33833395
goto error;

0 commit comments

Comments
 (0)