From 5624bf9758246cea3d40b3be70013f7d4411874d Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Thu, 25 Dec 2025 18:09:39 +0100 Subject: [PATCH] [3.13] gh-143145: Fix possible reference leak in ctypes _build_result() (GH-143131) (GH-143170) The result tuple was leaked if __ctypes_from_outparam__() failed for any item. (cherry picked from commit 579c5b496b467a2b175cb30caa4f6873cb13c9a1) Signed-off-by: Yongtao Huang Co-authored-by: Yongtao Huang --- .../next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst | 1 + Modules/_ctypes/_ctypes.c | 1 + 2 files changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst b/Misc/NEWS.d/next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst new file mode 100644 index 000000000000..2aff1090b181 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst @@ -0,0 +1 @@ +Fixed a possible reference leak in ctypes when constructing results with multiple output parameters on error. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index ad088b0ce8e2..7419d629399e 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4273,6 +4273,7 @@ _build_result(PyObject *result, PyObject *callargs, v = PyTuple_GET_ITEM(callargs, i); v = PyObject_CallMethodNoArgs(v, &_Py_ID(__ctypes_from_outparam__)); if (v == NULL || numretvals == 1) { + Py_XDECREF(tup); Py_DECREF(callargs); return v; } -- 2.47.3