From f9efd31ebce200fb1f65692fa4d23c981aa8ba66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 25 Jan 2025 11:50:02 +0100 Subject: [PATCH 1/3] fix UBSan failures for `ProfilerObject` --- Modules/_lsprof.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 51ad9fc7da8492..dd2c51a7203ac6 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -56,6 +56,8 @@ typedef struct { PyObject* missing; } ProfilerObject; +#define _ProfilerObject_CAST(op) ((ProfilerObject *)(op)) + #define POF_ENABLED 0x001 #define POF_SUBCALLS 0x002 #define POF_BUILTINS 0x004 @@ -919,29 +921,31 @@ _lsprof_Profiler_clear_impl(ProfilerObject *self) } static int -profiler_traverse(ProfilerObject *op, visitproc visit, void *arg) +profiler_traverse(PyObject *op, visitproc visit, void *arg) { + ProfilerObject *self = _ProfilerObject_CAST(op); Py_VISIT(Py_TYPE(op)); - Py_VISIT(op->externalTimer); + Py_VISIT(self->externalTimer); return 0; } static void -profiler_dealloc(ProfilerObject *op) +profiler_dealloc(PyObject *op) { - PyObject_GC_UnTrack(op); - if (op->flags & POF_ENABLED) { + ProfilerObject *self = _ProfilerObject_CAST(op); + PyObject_GC_UnTrack(self); + if (self->flags & POF_ENABLED) { PyThreadState *tstate = _PyThreadState_GET(); if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) { PyErr_FormatUnraisable("Exception ignored when destroying _lsprof profiler"); } } - flush_unmatched(op); - clearEntries(op); - Py_XDECREF(op->externalTimer); - PyTypeObject *tp = Py_TYPE(op); - tp->tp_free(op); + flush_unmatched(self); + clearEntries(self); + Py_XDECREF(self->externalTimer); + PyTypeObject *tp = Py_TYPE(self); + tp->tp_free(self); Py_DECREF(tp); } From 893b198c11ff7d9343d73521796cfcd226f03e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 25 Jan 2025 11:50:10 +0100 Subject: [PATCH 2/3] suppress unused return values --- Modules/_lsprof.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index dd2c51a7203ac6..878176dd630366 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -1046,7 +1046,7 @@ _lsprof_clear(PyObject *module) static void _lsprof_free(void *module) { - _lsprof_clear((PyObject *)module); + (void)_lsprof_clear((PyObject *)module); } static int From 411272576e0770d0d58a2c923bc3138606cb2ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 8 Feb 2025 10:11:08 +0100 Subject: [PATCH 3/3] Do not use `_` + capital letter in new code as it is also UB. --- Modules/_lsprof.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 1abbb0ee99a2f3..39d900cb824312 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -56,7 +56,7 @@ typedef struct { PyObject* missing; } ProfilerObject; -#define _ProfilerObject_CAST(op) ((ProfilerObject *)(op)) +#define ProfilerObject_CAST(op) ((ProfilerObject *)(op)) #define POF_ENABLED 0x001 #define POF_SUBCALLS 0x002 @@ -925,7 +925,7 @@ _lsprof_Profiler_clear_impl(ProfilerObject *self) static int profiler_traverse(PyObject *op, visitproc visit, void *arg) { - ProfilerObject *self = _ProfilerObject_CAST(op); + ProfilerObject *self = ProfilerObject_CAST(op); Py_VISIT(Py_TYPE(op)); Py_VISIT(self->externalTimer); return 0; @@ -934,7 +934,7 @@ profiler_traverse(PyObject *op, visitproc visit, void *arg) static void profiler_dealloc(PyObject *op) { - ProfilerObject *self = _ProfilerObject_CAST(op); + ProfilerObject *self = ProfilerObject_CAST(op); PyObject_GC_UnTrack(self); if (self->flags & POF_ENABLED) { PyThreadState *tstate = _PyThreadState_GET();