]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105375: Improve error handling in _ctypes (#105593)
authorErlend E. Aasland <erlend.aasland@protonmail.com>
Sun, 11 Jun 2023 19:46:19 +0000 (21:46 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Jun 2023 19:46:19 +0000 (19:46 +0000)
Prevent repeated PyLong_FromVoidPtr() from possibly overwriting the
current exception.

Misc/NEWS.d/next/Library/2023-06-09-21-40-45.gh-issue-105375._sZilh.rst [new file with mode: 0644]
Modules/_ctypes/callbacks.c

diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-40-45.gh-issue-105375._sZilh.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-40-45.gh-issue-105375._sZilh.rst
new file mode 100644 (file)
index 0000000..87db4c2
--- /dev/null
@@ -0,0 +1 @@
+Fix bugs in :mod:`_ctypes` where exceptions could end up being overwritten.
index 8e694ba852c1d4dcf3c098a81ed155126cb867c6..d71297f9c5c2f2eea5bd735ae67a9501c9649250 100644 (file)
@@ -479,12 +479,22 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
 
     {
         PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
+        if (py_rclsid == NULL) {
+            Py_DECREF(func);
+            PyErr_WriteUnraisable(context ? context : Py_None);
+            return E_FAIL;
+        }
         PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
+        if (py_riid == NULL) {
+            Py_DECREF(func);
+            Py_DECREF(py_rclsid);
+            PyErr_WriteUnraisable(context ? context : Py_None);
+            return E_FAIL;
+        }
         PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
-        if (!py_rclsid || !py_riid || !py_ppv) {
-            Py_XDECREF(py_rclsid);
-            Py_XDECREF(py_riid);
-            Py_XDECREF(py_ppv);
+        if (py_ppv == NULL) {
+            Py_DECREF(py_rclsid);
+            Py_DECREF(py_riid);
             Py_DECREF(func);
             PyErr_WriteUnraisable(context ? context : Py_None);
             return E_FAIL;