Skip to content

Commit a1d4b1c

Browse files
authored
gh-111178: fix UBSan failures in Modules/_lsprof.c (GH-129782)
Fix UBSan failures for `ProfilerObject` Suppress unused return value
1 parent 31ad8b6 commit a1d4b1c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Modules/_lsprof.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef struct {
5656
PyObject* missing;
5757
} ProfilerObject;
5858

59+
#define ProfilerObject_CAST(op) ((ProfilerObject *)(op))
60+
5961
#define POF_ENABLED 0x001
6062
#define POF_SUBCALLS 0x002
6163
#define POF_BUILTINS 0x004
@@ -921,30 +923,32 @@ _lsprof_Profiler_clear_impl(ProfilerObject *self)
921923
}
922924

923925
static int
924-
profiler_traverse(ProfilerObject *op, visitproc visit, void *arg)
926+
profiler_traverse(PyObject *op, visitproc visit, void *arg)
925927
{
928+
ProfilerObject *self = ProfilerObject_CAST(op);
926929
Py_VISIT(Py_TYPE(op));
927-
Py_VISIT(op->externalTimer);
930+
Py_VISIT(self->externalTimer);
928931
return 0;
929932
}
930933

931934
static void
932-
profiler_dealloc(ProfilerObject *op)
935+
profiler_dealloc(PyObject *op)
933936
{
934-
PyObject_GC_UnTrack(op);
935-
if (op->flags & POF_ENABLED) {
937+
ProfilerObject *self = ProfilerObject_CAST(op);
938+
PyObject_GC_UnTrack(self);
939+
if (self->flags & POF_ENABLED) {
936940
PyThreadState *tstate = _PyThreadState_GET();
937941
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
938942
PyErr_FormatUnraisable("Exception ignored while "
939943
"destroying _lsprof profiler");
940944
}
941945
}
942946

943-
flush_unmatched(op);
944-
clearEntries(op);
945-
Py_XDECREF(op->externalTimer);
946-
PyTypeObject *tp = Py_TYPE(op);
947-
tp->tp_free(op);
947+
flush_unmatched(self);
948+
clearEntries(self);
949+
Py_XDECREF(self->externalTimer);
950+
PyTypeObject *tp = Py_TYPE(self);
951+
tp->tp_free(self);
948952
Py_DECREF(tp);
949953
}
950954

@@ -1045,7 +1049,7 @@ _lsprof_clear(PyObject *module)
10451049
static void
10461050
_lsprof_free(void *module)
10471051
{
1048-
_lsprof_clear((PyObject *)module);
1052+
(void)_lsprof_clear((PyObject *)module);
10491053
}
10501054

10511055
static int

0 commit comments

Comments
 (0)