From 9d5f4f76b5a2e2552c9e268b0d85cc3b37ec883e Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Wed, 27 Aug 2025 01:03:25 +0800 Subject: [PATCH 1/3] gh-138008: fix segfault in _cyptes during _build_callargs Signed-off-by: Manjusaka --- Modules/_ctypes/_ctypes.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index ae49cbf3e57da7..121b3f32a490a1 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4382,7 +4382,11 @@ _build_callargs(ctypes_state *st, PyCFuncPtrObject *self, PyObject *argtypes, callargs = PyTuple_New(len); /* the argument tuple we build */ if (callargs == NULL) return NULL; - + if (!_validate_paramflags(st, Py_TYPE(self), argtypes)) { + PyErr_SetString(PyExc_TypeError, + "the current argument is invalid"); + goto error; + } #ifdef MS_WIN32 /* For a COM method, skip the first arg */ if (self->index) { From 5a03c30698415ca45be575ab73d749ae8b40cf26 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Wed, 27 Aug 2025 01:07:51 +0800 Subject: [PATCH 2/3] add test and news Signed-off-by: Manjusaka --- Lib/test/test_ctypes/test_funcptr.py | 8 ++++++++ .../2025-08-27-01-04-38.gh-issue-138008.iCEhZ9.rst | 1 + 2 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-08-27-01-04-38.gh-issue-138008.iCEhZ9.rst diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index b4c27809b5333d..b90f019dc67bed 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -199,6 +199,14 @@ def test_wrap_dll_function_str_ann(self): version = wrap_str_ann.Py_GetVersion() self.assertIsInstance(version, bytes) + def test_invalid_argtypes(self): + libc = CDLL(None) + + PRINTF_PROTO = CFUNCTYPE(c_int, ctypes.c_char_p) + c_printf = PRINTF_PROTO(("printf", libc), ((1,),)) + c_printf.argtypes = (c_char_p, c_int) + with self.assertRaises(TypeError): + c_printf(b"Hello\n") if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2025-08-27-01-04-38.gh-issue-138008.iCEhZ9.rst b/Misc/NEWS.d/next/Library/2025-08-27-01-04-38.gh-issue-138008.iCEhZ9.rst new file mode 100644 index 00000000000000..53e394547de84a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-27-01-04-38.gh-issue-138008.iCEhZ9.rst @@ -0,0 +1 @@ +Fix segfaults in _cyptes during _build_callargs. From e84ca2a46d8757d8f5849f3e520781f4d0de5ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 21 Oct 2026 03:20:31 +0200 Subject: [PATCH 3/3] Fix CI: pass paramflags to _validate_paramflags (gh-138008) Co-authored-by: Muse Code (Meta Muse Spark) --- Modules/_ctypes/_ctypes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 121b3f32a490a1..f6bf40c07169a4 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4382,7 +4382,7 @@ _build_callargs(ctypes_state *st, PyCFuncPtrObject *self, PyObject *argtypes, callargs = PyTuple_New(len); /* the argument tuple we build */ if (callargs == NULL) return NULL; - if (!_validate_paramflags(st, Py_TYPE(self), argtypes)) { + if (!_validate_paramflags(st, Py_TYPE(self), paramflags, argtypes)) { PyErr_SetString(PyExc_TypeError, "the current argument is invalid"); goto error;