Skip to content

gh-111178: fix UBSan failures for Modules/_testcapi/*.c #131613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions Modules/_testcapi/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typedef struct {
Py_ssize_t references;
} testBufObject;

#define testBufObject_CAST(op) ((testBufObject *)(op))

static PyObject *
testbuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Expand All @@ -29,30 +31,34 @@ testbuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

static int
testbuf_traverse(testBufObject *self, visitproc visit, void *arg)
testbuf_traverse(PyObject *op, visitproc visit, void *arg)
{
testBufObject *self = testBufObject_CAST(op);
Py_VISIT(self->obj);
return 0;
}

static int
testbuf_clear(testBufObject *self)
testbuf_clear(PyObject *op)
{
testBufObject *self = testBufObject_CAST(op);
Py_CLEAR(self->obj);
return 0;
}

static void
testbuf_dealloc(testBufObject *self)
testbuf_dealloc(PyObject *op)
{
testBufObject *self = testBufObject_CAST(op);
PyObject_GC_UnTrack(self);
Py_XDECREF(self->obj);
Py_TYPE(self)->tp_free((PyObject *) self);
Py_TYPE(self)->tp_free(self);
}

static int
testbuf_getbuf(testBufObject *self, Py_buffer *view, int flags)
testbuf_getbuf(PyObject *op, Py_buffer *view, int flags)
{
testBufObject *self = testBufObject_CAST(op);
int buf = PyObject_GetBuffer(self->obj, view, flags);
if (buf == 0) {
Py_SETREF(view->obj, Py_NewRef(self));
Expand All @@ -62,15 +68,16 @@ testbuf_getbuf(testBufObject *self, Py_buffer *view, int flags)
}

static void
testbuf_releasebuf(testBufObject *self, Py_buffer *view)
testbuf_releasebuf(PyObject *op, Py_buffer *Py_UNUSED(view))
{
testBufObject *self = testBufObject_CAST(op);
self->references--;
assert(self->references >= 0);
}

static PyBufferProcs testbuf_as_buffer = {
.bf_getbuffer = (getbufferproc) testbuf_getbuf,
.bf_releasebuffer = (releasebufferproc) testbuf_releasebuf,
.bf_getbuffer = testbuf_getbuf,
.bf_releasebuffer = testbuf_releasebuf,
};

static struct PyMemberDef testbuf_members[] = {
Expand All @@ -84,9 +91,9 @@ static PyTypeObject testBufType = {
.tp_basicsize = sizeof(testBufObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_new = testbuf_new,
.tp_dealloc = (destructor) testbuf_dealloc,
.tp_traverse = (traverseproc) testbuf_traverse,
.tp_clear = (inquiry) testbuf_clear,
.tp_dealloc = testbuf_dealloc,
.tp_traverse = testbuf_traverse,
.tp_clear = testbuf_clear,
.tp_as_buffer = &testbuf_as_buffer,
.tp_members = testbuf_members
};
Expand Down
11 changes: 7 additions & 4 deletions Modules/_testcapi/monitoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct {
/* Other fields */
} PyCodeLikeObject;

#define PyCodeLikeObject_CAST(op) ((PyCodeLikeObject *)(op))

static PyObject *
CodeLike_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Expand All @@ -40,17 +41,19 @@ CodeLike_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

static void
CodeLike_dealloc(PyCodeLikeObject *self)
CodeLike_dealloc(PyObject *op)
{
PyCodeLikeObject *self = PyCodeLikeObject_CAST(op);
if (self->monitoring_states) {
PyMem_Free(self->monitoring_states);
}
Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *
CodeLike_str(PyCodeLikeObject *self)
CodeLike_str(PyObject *op)
{
PyCodeLikeObject *self = PyCodeLikeObject_CAST(op);
PyObject *res = NULL;
PyObject *sep = NULL;
PyObject *parts = NULL;
Expand Down Expand Up @@ -101,8 +104,8 @@ static PyTypeObject PyCodeLike_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = CodeLike_new,
.tp_dealloc = (destructor) CodeLike_dealloc,
.tp_str = (reprfunc) CodeLike_str,
.tp_dealloc = CodeLike_dealloc,
.tp_str = CodeLike_str,
};

#define RAISE_UNLESS_CODELIKE(v) if (!Py_IS_TYPE((v), &PyCodeLike_Type)) { \
Expand Down
Loading