Skip to content

Commit 1089631

Browse files
erlend-aaslandpull[bot]
authored andcommitted
gh-105375: Improve error handling in the sys extension module (#105611)
In _PySys_AddXOptionWithError() and sys_add_xoption(), bail on first error to prevent exceptions from possibly being overwritten.
1 parent a89dbf8 commit 1089631

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed
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

+18-6
Original file line numberDiff line numberDiff line change
@@ -2721,14 +2721,20 @@ _PySys_AddXOptionWithError(const wchar_t *s)
27212721
const wchar_t *name_end = wcschr(s, L'=');
27222722
if (!name_end) {
27232723
name = PyUnicode_FromWideChar(s, -1);
2724+
if (name == NULL) {
2725+
goto error;
2726+
}
27242727
value = Py_NewRef(Py_True);
27252728
}
27262729
else {
27272730
name = PyUnicode_FromWideChar(s, name_end - s);
2731+
if (name == NULL) {
2732+
goto error;
2733+
}
27282734
value = PyUnicode_FromWideChar(name_end + 1, -1);
2729-
}
2730-
if (name == NULL || value == NULL) {
2731-
goto error;
2735+
if (value == NULL) {
2736+
goto error;
2737+
}
27322738
}
27332739
if (PyDict_SetItem(opts, name, value) < 0) {
27342740
goto error;
@@ -3374,14 +3380,20 @@ sys_add_xoption(PyObject *opts, const wchar_t *s)
33743380
const wchar_t *name_end = wcschr(s, L'=');
33753381
if (!name_end) {
33763382
name = PyUnicode_FromWideChar(s, -1);
3383+
if (name == NULL) {
3384+
goto error;
3385+
}
33773386
value = Py_NewRef(Py_True);
33783387
}
33793388
else {
33803389
name = PyUnicode_FromWideChar(s, name_end - s);
3390+
if (name == NULL) {
3391+
goto error;
3392+
}
33813393
value = PyUnicode_FromWideChar(name_end + 1, -1);
3382-
}
3383-
if (name == NULL || value == NULL) {
3384-
goto error;
3394+
if (value == NULL) {
3395+
goto error;
3396+
}
33853397
}
33863398
if (PyDict_SetItem(opts, name, value) < 0) {
33873399
goto error;

0 commit comments

Comments
 (0)