From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sun, 11 Jun 2023 20:30:47 +0000 (-0700) Subject: [3.11] gh-105375: Improve error handling in _ctypes (GH-105593) (#105664) X-Git-Tag: v3.11.5~286 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a03449374ebd5bffe54d3e09d36b54bc605551d8;p=thirdparty%2FPython%2Fcpython.git [3.11] gh-105375: Improve error handling in _ctypes (GH-105593) (#105664) Prevent repeated PyLong_FromVoidPtr() from possibly overwriting the current exception. (cherry picked from commit e8998e46a7ce8ad336e0941a6da6e50cb88d1e47) Co-authored-by: Erlend E. Aasland --- 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 index 000000000000..87db4c2b4e22 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-40-45.gh-issue-105375._sZilh.rst @@ -0,0 +1 @@ +Fix bugs in :mod:`_ctypes` where exceptions could end up being overwritten. diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index b46067bf2fa6..e2dc2d23317f 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -499,12 +499,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;