From 3eb6a1550eb1743546f09bda32a5fef5e80ec339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20G=C3=B6rgens?= Date: Mon, 19 Sep 2022 15:59:13 +0800 Subject: [PATCH] gh-96821: Fix undefined behaviour in `_testcapimodule.c` (GH-96915) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * gh-96821: Assert for demonstrating undefined behaviour * Fix UB Co-authored-by: C.A.M. Gerlach (cherry picked from commit cbdeda8ce7a3543cb3376d70e4cd46fcf24f42a7) Co-authored-by: Matthias Görgens --- .../2022-09-18-08-47-40.gh-issue-96821.Co2iOq.rst | 1 + Modules/_testcapimodule.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-09-18-08-47-40.gh-issue-96821.Co2iOq.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-18-08-47-40.gh-issue-96821.Co2iOq.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-18-08-47-40.gh-issue-96821.Co2iOq.rst new file mode 100644 index 00000000000000..4fd0532e827d18 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-18-08-47-40.gh-issue-96821.Co2iOq.rst @@ -0,0 +1 @@ +Fix undefined behaviour in ``_testcapimodule.c``. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 1a3cbe0b212aa6..43fec8138a0ebc 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5576,8 +5576,10 @@ meth_fastcall_keywords(PyObject* self, PyObject* const* args, if (pyargs == NULL) { return NULL; } + assert(args != NULL || nargs == 0); + PyObject* const* args_offset = args == NULL ? NULL : args + nargs; PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type, - args + nargs, 0, kwargs); + args_offset, 0, kwargs); return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs); }