From 7ac735da86525b502cf527800967ec71926056f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:12:54 +0100 Subject: [PATCH 1/2] fix UBSan failures for `Modules/_testbuffer.c` --- Modules/_testbuffer.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c index 7ecb11da2bb6af..363884443cff8f 100644 --- a/Modules/_testbuffer.c +++ b/Modules/_testbuffer.c @@ -1586,8 +1586,9 @@ ptr_from_index(Py_buffer *base, Py_ssize_t index) } static PyObject * -ndarray_item(NDArrayObject *self, Py_ssize_t index) +ndarray_item(PyObject *op, Py_ssize_t index) { + NDArrayObject *self = (NDArrayObject *)op; ndbuf_t *ndbuf = self->head; Py_buffer *base = &ndbuf->base; char *ptr; @@ -1801,7 +1802,7 @@ ndarray_subscript(PyObject *op, PyObject *key) Py_ssize_t index = PyLong_AsSsize_t(key); if (index == -1 && PyErr_Occurred()) return NULL; - return ndarray_item(self, index); + return ndarray_item(op, index); } nd = (NDArrayObject *)ndarray_new(&NDArray_Type, NULL, NULL); @@ -1966,10 +1967,10 @@ static PyMappingMethods ndarray_as_mapping = { }; static PySequenceMethods ndarray_as_sequence = { - 0, /* sq_length */ - 0, /* sq_concat */ - 0, /* sq_repeat */ - (ssizeargfunc)ndarray_item, /* sq_item */ + 0, /* sq_length */ + 0, /* sq_concat */ + 0, /* sq_repeat */ + ndarray_item, /* sq_item */ }; @@ -2742,7 +2743,7 @@ staticarray_init(PyObject *self, PyObject *args, PyObject *kwds) } static void -staticarray_dealloc(StaticArrayObject *self) +staticarray_dealloc(PyObject *self) { PyObject_Free(self); } @@ -2750,8 +2751,9 @@ staticarray_dealloc(StaticArrayObject *self) /* Return a buffer for a PyBUF_FULL_RO request. Flags are not checked, which makes this object a non-compliant exporter! */ static int -staticarray_getbuf(StaticArrayObject *self, Py_buffer *view, int flags) +staticarray_getbuf(PyObject *op, Py_buffer *view, int flags) { + StaticArrayObject *a = (StaticArrayObject *)op; *view = static_buffer; if (self->legacy_mode) { @@ -2765,7 +2767,7 @@ staticarray_getbuf(StaticArrayObject *self, Py_buffer *view, int flags) } static PyBufferProcs staticarray_as_buffer = { - (getbufferproc)staticarray_getbuf, /* bf_getbuffer */ + staticarray_getbuf, /* bf_getbuffer */ NULL, /* bf_releasebuffer */ }; @@ -2774,7 +2776,7 @@ static PyTypeObject StaticArray_Type = { "staticarray", /* Name of this type */ sizeof(StaticArrayObject), /* Basic object size */ 0, /* Item size for varobject */ - (destructor)staticarray_dealloc, /* tp_dealloc */ + staticarray_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ From a9f306a28fd4854c4876d6fb70c4495668401446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:16:14 +0100 Subject: [PATCH 2/2] fixup --- Modules/_testbuffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c index 363884443cff8f..7fc4d61db29469 100644 --- a/Modules/_testbuffer.c +++ b/Modules/_testbuffer.c @@ -2753,7 +2753,7 @@ staticarray_dealloc(PyObject *self) static int staticarray_getbuf(PyObject *op, Py_buffer *view, int flags) { - StaticArrayObject *a = (StaticArrayObject *)op; + StaticArrayObject *self = (StaticArrayObject *)op; *view = static_buffer; if (self->legacy_mode) {