]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-105375: Improve error handling in the sys extension module (#105611) (...
authorErlend E. Aasland <erlend.aasland@protonmail.com>
Sun, 11 Jun 2023 21:08:40 +0000 (23:08 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Jun 2023 21:08:40 +0000 (21:08 +0000)
(cherry picked from commit 41cddc2e93a285b81fa30ac542b088bd9d0112e9)

In _PySys_AddXOptionWithError() and sys_add_xoption(),
bail on first error to prevent exceptions from possibly being
overwritten.

Misc/NEWS.d/next/Library/2023-06-09-23-46-23.gh-issue-105375.9KaioS.rst [new file with mode: 0644]
Python/sysmodule.c

diff --git a/Misc/NEWS.d/next/Library/2023-06-09-23-46-23.gh-issue-105375.9KaioS.rst b/Misc/NEWS.d/next/Library/2023-06-09-23-46-23.gh-issue-105375.9KaioS.rst
new file mode 100644 (file)
index 0000000..b12d7c8
--- /dev/null
@@ -0,0 +1,2 @@
+Fix bugs in :mod:`sys` where exceptions could end up being overwritten
+because of deferred error handling.
index 8bab7037138d320c3576e9cc32ce8841f3ba56a4..84f17389559cf6e4068bf3dfa8d0e262d9b1d534 100644 (file)
@@ -2360,15 +2360,21 @@ _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_True;
         Py_INCREF(value);
     }
     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;
@@ -3019,15 +3025,21 @@ 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_True;
         Py_INCREF(value);
     }
     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;