Skip to content
Merged
Prev Previous commit
Next Next commit
fix UBSan failures for XIBufferViewObject
  • Loading branch information
picnixz committed Jan 25, 2025
commit 7dad5529e4d6d532f3d0cb957d5b7a7225a2b6fd
14 changes: 9 additions & 5 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ typedef struct {
int64_t interpid;
} XIBufferViewObject;

#define _XIBufferViewObject_CAST(op) ((XIBufferViewObject *)(op))

static PyObject *
xibufferview_from_xid(PyTypeObject *cls, _PyXIData_t *data)
{
Expand All @@ -100,8 +102,9 @@ xibufferview_from_xid(PyTypeObject *cls, _PyXIData_t *data)
}

static void
xibufferview_dealloc(XIBufferViewObject *self)
xibufferview_dealloc(PyObject *op)
{
XIBufferViewObject *self = _XIBufferViewObject_CAST(op);
PyInterpreterState *interp = _PyInterpreterState_LookUpID(self->interpid);
/* If the interpreter is no longer alive then we have problems,
since other objects may be using the buffer still. */
Expand All @@ -124,20 +127,21 @@ xibufferview_dealloc(XIBufferViewObject *self)
}

static int
xibufferview_getbuf(XIBufferViewObject *self, Py_buffer *view, int flags)
xibufferview_getbuf(PyObject *op, Py_buffer *view, int flags)
{
/* Only PyMemoryView_FromObject() should ever call this,
via _memoryview_from_xid() below. */
XIBufferViewObject *self = _XIBufferViewObject_CAST(op);
*view = *self->view;
view->obj = (PyObject *)self;
view->obj = op;
// XXX Should we leave it alone?
view->internal = NULL;
return 0;
}

static PyType_Slot XIBufferViewType_slots[] = {
{Py_tp_dealloc, (destructor)xibufferview_dealloc},
{Py_bf_getbuffer, (getbufferproc)xibufferview_getbuf},
{Py_tp_dealloc, xibufferview_dealloc},
{Py_bf_getbuffer, xibufferview_getbuf},
// We don't bother with Py_bf_releasebuffer since we don't need it.
{0, NULL},
};
Expand Down