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.
This commit is contained in:
Erlend E. Aasland 2023-06-11 22:02:49 +02:00 committed by GitHub
parent e8998e46a7
commit 41cddc2e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -0,0 +1,2 @@
Fix bugs in :mod:`sys` where exceptions could end up being overwritten
because of deferred error handling.

View File

@ -2721,14 +2721,20 @@ _PySys_AddXOptionWithError(const wchar_t *s)
const wchar_t *name_end = wcschr(s, L'=');
if (!name_end) {
name = PyUnicode_FromWideChar(s, -1);
if (name == NULL) {
goto error;
}
value = Py_NewRef(Py_True);
}
else {
name = PyUnicode_FromWideChar(s, name_end - s);
if (name == NULL) {
goto error;
}
value = PyUnicode_FromWideChar(name_end + 1, -1);
}
if (name == NULL || value == NULL) {
goto error;
if (value == NULL) {
goto error;
}
}
if (PyDict_SetItem(opts, name, value) < 0) {
goto error;
@ -3374,14 +3380,20 @@ sys_add_xoption(PyObject *opts, const wchar_t *s)
const wchar_t *name_end = wcschr(s, L'=');
if (!name_end) {
name = PyUnicode_FromWideChar(s, -1);
if (name == NULL) {
goto error;
}
value = Py_NewRef(Py_True);
}
else {
name = PyUnicode_FromWideChar(s, name_end - s);
if (name == NULL) {
goto error;
}
value = PyUnicode_FromWideChar(name_end + 1, -1);
}
if (name == NULL || value == NULL) {
goto error;
if (value == NULL) {
goto error;
}
}
if (PyDict_SetItem(opts, name, value) < 0) {
goto error;