gh-111178: fix UBSan failures for `Modules/_testmultiphase.c` (#131615) · python/cpython@4519179

@@ -28,6 +28,8 @@ typedef struct {

2828

PyObject *x_attr; /* Attributes dictionary */

2929

} ExampleObject;

303031+

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

32+3133

typedef struct {

3234

PyObject *integer;

3335

} testmultiphase_state;

@@ -39,20 +41,22 @@ typedef struct {

3941

/* Example methods */

40424143

static int

42-

Example_traverse(ExampleObject *self, visitproc visit, void *arg)

44+

Example_traverse(PyObject *op, visitproc visit, void *arg)

4345

{

46+

ExampleObject *self = ExampleObject_CAST(op);

4447

Py_VISIT(self->x_attr);

4548

return 0;

4649

}

47504851

static void

49-

Example_finalize(ExampleObject *self)

52+

Example_finalize(PyObject *op)

5053

{

54+

ExampleObject *self = ExampleObject_CAST(op);

5155

Py_CLEAR(self->x_attr);

5256

}

53575458

static PyObject *

55-

Example_demo(ExampleObject *self, PyObject *args)

59+

Example_demo(PyObject *op, PyObject *args)

5660

{

5761

PyObject *o = NULL;

5862

if (!PyArg_ParseTuple(args, "|O:demo", &o))

@@ -66,14 +70,15 @@ Example_demo(ExampleObject *self, PyObject *args)

6670

#include "clinic/_testmultiphase.c.h"

67716872

static PyMethodDef Example_methods[] = {

69-

{"demo", (PyCFunction)Example_demo, METH_VARARGS,

73+

{"demo", Example_demo, METH_VARARGS,

7074

PyDoc_STR("demo() -> None")},

7175

{NULL, NULL} /* sentinel */

7276

};

73777478

static PyObject *

75-

Example_getattro(ExampleObject *self, PyObject *name)

79+

Example_getattro(PyObject *op, PyObject *name)

7680

{

81+

ExampleObject *self = ExampleObject_CAST(op);

7782

if (self->x_attr != NULL) {

7883

PyObject *v = PyDict_GetItemWithError(self->x_attr, name);

7984

if (v != NULL) {

@@ -87,8 +92,9 @@ Example_getattro(ExampleObject *self, PyObject *name)

8792

}

88938994

static int

90-

Example_setattr(ExampleObject *self, const char *name, PyObject *v)

95+

Example_setattr(PyObject *op, char *name, PyObject *v)

9196

{

97+

ExampleObject *self = ExampleObject_CAST(op);

9298

if (self->x_attr == NULL) {

9399

self->x_attr = PyDict_New();

94100

if (self->x_attr == NULL)

@@ -212,7 +218,7 @@ PyDoc_STRVAR(_StateAccessType_decrement_count__doc__,

212218213219

// Intentionally does not use Argument Clinic

214220

static PyObject *

215-

_StateAccessType_increment_count_noclinic(StateAccessTypeObject *self,

221+

_StateAccessType_increment_count_noclinic(PyObject *self,

216222

PyTypeObject *defining_class,

217223

PyObject *const *args,

218224

Py_ssize_t nargs,