From adc184e4c3c471d41267a7b94a1e3a9d36bc704a Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Fri, 14 Nov 2025 16:03:54 +0300 Subject: [PATCH 1/4] Fix incorrect function signatures in `_testmultiphase` --- .../C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst | 2 ++ Modules/_testmultiphase.c | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst diff --git a/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst b/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst new file mode 100644 index 00000000000000..df4ed3f12cd19c --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst @@ -0,0 +1,2 @@ +Fix UBSan-reported incorrect function signatures in ``_testmultiphase`` +(patched by Shamil Abdulaev). diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index 220fa888e49a52..f2fe0c15adcdbb 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -1061,7 +1061,7 @@ PyModInit__test_from_modexport_exception(void) } static PyObject * -modexport_create_string(PyObject *spec, PyObject *def) +modexport_create_string(PyObject *spec, PyModuleDef *def) { assert(def == NULL); return PyUnicode_FromString("is this \xf0\x9f\xa6\x8b... a module?"); @@ -1138,11 +1138,12 @@ modexport_get_empty_slots(PyObject *mod, PyObject *arg) } static void -modexport_smoke_free(PyObject *mod) +modexport_smoke_free(void *mod_state) { - int *state = PyModule_GetState(mod); + int *state = (int *)mod_state; if (!state) { - PyErr_FormatUnraisable("Exception ignored in module %R free", mod); + PyErr_FormatUnraisable("Exception ignored in module state free", NULL); + return; } assert(*state == 258); } From e1e0b0039a79e52d867cde96df152493f4b74456 Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Fri, 14 Nov 2025 16:10:21 +0300 Subject: [PATCH 2/4] fix code --- Modules/_testmultiphase.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index f2fe0c15adcdbb..85739ff1656b72 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -1138,11 +1138,13 @@ modexport_get_empty_slots(PyObject *mod, PyObject *arg) } static void -modexport_smoke_free(void *mod_state) +modexport_smoke_free(void *mod) { - int *state = (int *)mod_state; + int *state = PyModule_GetState((PyObject *)mod); if (!state) { - PyErr_FormatUnraisable("Exception ignored in module state free", NULL); + PyErr_FormatUnraisable( + "Exception ignored in module %R free", (PyObject *)mod + ); return; } assert(*state == 258); From 838d9843f9bfd25d30603e3156301b208435e7fc Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Fri, 14 Nov 2025 17:11:24 +0300 Subject: [PATCH 3/4] remove blurb --- .../next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst diff --git a/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst b/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst deleted file mode 100644 index df4ed3f12cd19c..00000000000000 --- a/Misc/NEWS.d/next/C_API/2025-11-14-16-02-59.gh-issue-141553.IsXJlH.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix UBSan-reported incorrect function signatures in ``_testmultiphase`` -(patched by Shamil Abdulaev). From 587ac0c2a3525cd01590a2cc5251eb7074f3b3d5 Mon Sep 17 00:00:00 2001 From: Shamil Date: Sat, 15 Nov 2025 16:50:47 +0300 Subject: [PATCH 4/4] Refactor modexport_smoke_free function parameters --- Modules/_testmultiphase.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index 85739ff1656b72..cd2d7b65598277 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -1138,14 +1138,12 @@ modexport_get_empty_slots(PyObject *mod, PyObject *arg) } static void -modexport_smoke_free(void *mod) +modexport_smoke_free(void *op) { - int *state = PyModule_GetState((PyObject *)mod); + PyObject *mod = (PyObject *)op; + int *state = PyModule_GetState(mod); if (!state) { - PyErr_FormatUnraisable( - "Exception ignored in module %R free", (PyObject *)mod - ); - return; + PyErr_FormatUnraisable("Exception ignored in module %R free", mod); } assert(*state == 258); }