From dad8d9b8000e38831564199eb0177dae1e9e9765 Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Wed, 24 Dec 2025 14:07:08 +0800 Subject: [PATCH 1/3] Fix reference leak in ctypes _build_result() In _ctypes._build_result(), a result tuple is allocated when numretvals > 1. If v becomes NULL during result construction, the function returns early without DECREF'ing the tuple, leaking a reference. Add Py_XDECREF(tup) before returning when numretvals > 1 and v == NULL. Signed-off-by: Yongtao Huang --- Modules/_ctypes/_ctypes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 774ac71ce9ec56..563e95a762599b 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4557,6 +4557,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; } From 89011b5e1860db5335b1163f75ea91f2d435e43f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 24 Dec 2025 14:18:53 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst | 1 + 1 file changed, 1 insertion(+) 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 00000000000000..c7b8576c2c3ba5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst @@ -0,0 +1 @@ +Fixed a reference leak in ctypes when building results with multiple out parameters. From 925b1f96b1412d391c09c18cb5c6b5128c149566 Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 21:58:55 +0800 Subject: [PATCH 3/3] Update News.d --- .../next/Library/2025-12-24-14-18-52.gh-issue-143145.eXLw8D.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c7b8576c2c3ba5..2aff1090b1812f 100644 --- 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 @@ -1 +1 @@ -Fixed a reference leak in ctypes when building results with multiple out parameters. +Fixed a possible reference leak in ctypes when constructing results with multiple output parameters on error.