[Python-checkins] [3.12] gh-105375: Improve error handling in the sys extension module (GH-105611) (#105665)

erlend-aasland webhook-mailer at python.org
Sun Jun 11 17:12:24 EDT 2023


https://github.com/python/cpython/commit/a1034b5fd3af59d813452742153e60b40b39ff4a
commit: a1034b5fd3af59d813452742153e60b40b39ff4a
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-06-11T21:12:17Z
summary:

[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 41cddc2e93a285b81fa30ac542b088bd9d0112e9)

Co-authored-by: Erlend E. Aasland <erlend.aasland at protonmail.com>

files:
A Misc/NEWS.d/next/Library/2023-06-09-23-46-23.gh-issue-105375.9KaioS.rst
M 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
index 000000000000..b12d7c864e7b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-06-09-23-46-23.gh-issue-105375.9KaioS.rst
@@ -0,0 +1,2 @@
+Fix bugs in :mod:`sys` where exceptions could end up being overwritten
+because of deferred error handling.
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index e6731e7565fc..a05bdf0ea7ee 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -2718,14 +2718,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;
@@ -3370,14 +3376,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;



More information about the Python-checkins mailing list