]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111178: fix UBSan failures for `Python/instrumentation.c` (#131608)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Mon, 24 Mar 2025 09:58:33 +0000 (10:58 +0100)
committerGitHub <noreply@github.com>
Mon, 24 Mar 2025 09:58:33 +0000 (10:58 +0100)
Python/instrumentation.c

index 9bf0fc435568039cdf1b740813eb0e7bf83a6469..bcf7267d23b2e30ac9b8acbfd78b7fef6be72545 100644 (file)
@@ -2916,18 +2916,21 @@ typedef struct _PyLegacyBranchEventHandler {
     int tool_id;
 } _PyLegacyBranchEventHandler;
 
+#define _PyLegacyBranchEventHandler_CAST(op)    ((_PyLegacyBranchEventHandler *)(op))
+
 static void
-dealloc_branch_handler(_PyLegacyBranchEventHandler *self)
+dealloc_branch_handler(PyObject *op)
 {
+    _PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
     Py_CLEAR(self->handler);
-    PyObject_Free((PyObject *)self);
+    PyObject_Free(self);
 }
 
 static PyTypeObject _PyLegacyBranchEventHandler_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "sys.monitoring.branch_event_handler",
     sizeof(_PyLegacyBranchEventHandler),
-    .tp_dealloc = (destructor)dealloc_branch_handler,
+    .tp_dealloc = dealloc_branch_handler,
     .tp_vectorcall_offset = offsetof(_PyLegacyBranchEventHandler, vectorcall),
     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
         Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DISALLOW_INSTANTIATION,
@@ -2936,10 +2939,11 @@ static PyTypeObject _PyLegacyBranchEventHandler_Type = {
 
 
 static PyObject *
-branch_handler(
-    _PyLegacyBranchEventHandler *self, PyObject *const *args,
+branch_handler_vectorcall(
+    PyObject *op, PyObject *const *args,
     size_t nargsf, PyObject *kwnames
 ) {
+    _PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
     // Find the other instrumented instruction and remove tool
     // The spec (PEP 669) allows spurious events after a DISABLE,
     // so a best effort is good enough.
@@ -3000,7 +3004,7 @@ static PyObject *make_branch_handler(int tool_id, PyObject *handler, bool right)
     if (callback == NULL) {
         return NULL;
     }
-    callback->vectorcall = (vectorcallfunc)branch_handler;
+    callback->vectorcall = branch_handler_vectorcall;
     callback->handler = Py_NewRef(handler);
     callback->right = right;
     callback->tool_id = tool_id;
@@ -3062,6 +3066,8 @@ typedef struct {
     int bi_offset;
 } branchesiterator;
 
+#define branchesiterator_CAST(op)   ((branchesiterator *)(op))
+
 static PyObject *
 int_triple(int a, int b, int c) {
     PyObject *obja = PyLong_FromLong(a);
@@ -3088,8 +3094,9 @@ error:
 }
 
 static PyObject *
-branchesiter_next(branchesiterator *bi)
+branchesiter_next(PyObject *op)
 {
+    branchesiterator *bi = branchesiterator_CAST(op);
     int offset = bi->bi_offset;
     int oparg = 0;
     while (offset < Py_SIZE(bi->bi_code)) {
@@ -3130,8 +3137,9 @@ branchesiter_next(branchesiterator *bi)
 }
 
 static void
-branchesiter_dealloc(branchesiterator *bi)
+branchesiter_dealloc(PyObject *op)
 {
+    branchesiterator *bi = branchesiterator_CAST(op);
     Py_DECREF(bi->bi_code);
     PyObject_Free(bi);
 }
@@ -3142,10 +3150,10 @@ static PyTypeObject _PyBranchesIterator = {
     sizeof(branchesiterator),           /* tp_basicsize */
     0,                                  /* tp_itemsize */
     /* methods */
-    .tp_dealloc = (destructor)branchesiter_dealloc,
+    .tp_dealloc = branchesiter_dealloc,
     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
     .tp_iter = PyObject_SelfIter,
-    .tp_iternext = (iternextfunc)branchesiter_next,
+    .tp_iternext = branchesiter_next,
     .tp_free = PyObject_Del,
 };