Skip to content

gh-111178: fix UBSan failures for Modules/_testmultiphase.c #131615

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
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
20 changes: 13 additions & 7 deletions Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct {
PyObject *x_attr; /* Attributes dictionary */
} ExampleObject;

#define ExampleObject_CAST(op) ((ExampleObject *)(op))

typedef struct {
PyObject *integer;
} testmultiphase_state;
Expand All @@ -39,20 +41,22 @@ typedef struct {
/* Example methods */

static int
Example_traverse(ExampleObject *self, visitproc visit, void *arg)
Example_traverse(PyObject *op, visitproc visit, void *arg)
{
ExampleObject *self = ExampleObject_CAST(op);
Py_VISIT(self->x_attr);
return 0;
}

static void
Example_finalize(ExampleObject *self)
Example_finalize(PyObject *op)
{
ExampleObject *self = ExampleObject_CAST(op);
Py_CLEAR(self->x_attr);
}

static PyObject *
Example_demo(ExampleObject *self, PyObject *args)
Example_demo(PyObject *op, PyObject *args)
{
PyObject *o = NULL;
if (!PyArg_ParseTuple(args, "|O:demo", &o))
Expand All @@ -66,14 +70,15 @@ Example_demo(ExampleObject *self, PyObject *args)
#include "clinic/_testmultiphase.c.h"

static PyMethodDef Example_methods[] = {
{"demo", (PyCFunction)Example_demo, METH_VARARGS,
{"demo", Example_demo, METH_VARARGS,
PyDoc_STR("demo() -> None")},
{NULL, NULL} /* sentinel */
};

static PyObject *
Example_getattro(ExampleObject *self, PyObject *name)
Example_getattro(PyObject *op, PyObject *name)
{
ExampleObject *self = ExampleObject_CAST(op);
if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) {
Expand All @@ -87,8 +92,9 @@ Example_getattro(ExampleObject *self, PyObject *name)
}

static int
Example_setattr(ExampleObject *self, const char *name, PyObject *v)
Example_setattr(PyObject *op, char *name, PyObject *v)
{
ExampleObject *self = ExampleObject_CAST(op);
if (self->x_attr == NULL) {
self->x_attr = PyDict_New();
if (self->x_attr == NULL)
Expand Down Expand Up @@ -212,7 +218,7 @@ PyDoc_STRVAR(_StateAccessType_decrement_count__doc__,

// Intentionally does not use Argument Clinic
static PyObject *
_StateAccessType_increment_count_noclinic(StateAccessTypeObject *self,
_StateAccessType_increment_count_noclinic(PyObject *self,
PyTypeObject *defining_class,
PyObject *const *args,
Py_ssize_t nargs,
Expand Down
Loading