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

This commit is contained in:
Bénédikt Tran 2025-03-24 10:59:30 +01:00 committed by GitHub
parent 43fde78bef
commit 45191797d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,6 +28,8 @@ typedef struct {
PyObject *x_attr; /* Attributes dictionary */ PyObject *x_attr; /* Attributes dictionary */
} ExampleObject; } ExampleObject;
#define ExampleObject_CAST(op) ((ExampleObject *)(op))
typedef struct { typedef struct {
PyObject *integer; PyObject *integer;
} testmultiphase_state; } testmultiphase_state;
@ -39,20 +41,22 @@ typedef struct {
/* Example methods */ /* Example methods */
static int 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); Py_VISIT(self->x_attr);
return 0; return 0;
} }
static void static void
Example_finalize(ExampleObject *self) Example_finalize(PyObject *op)
{ {
ExampleObject *self = ExampleObject_CAST(op);
Py_CLEAR(self->x_attr); Py_CLEAR(self->x_attr);
} }
static PyObject * static PyObject *
Example_demo(ExampleObject *self, PyObject *args) Example_demo(PyObject *op, PyObject *args)
{ {
PyObject *o = NULL; PyObject *o = NULL;
if (!PyArg_ParseTuple(args, "|O:demo", &o)) if (!PyArg_ParseTuple(args, "|O:demo", &o))
@ -66,14 +70,15 @@ Example_demo(ExampleObject *self, PyObject *args)
#include "clinic/_testmultiphase.c.h" #include "clinic/_testmultiphase.c.h"
static PyMethodDef Example_methods[] = { static PyMethodDef Example_methods[] = {
{"demo", (PyCFunction)Example_demo, METH_VARARGS, {"demo", Example_demo, METH_VARARGS,
PyDoc_STR("demo() -> None")}, PyDoc_STR("demo() -> None")},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static PyObject * static PyObject *
Example_getattro(ExampleObject *self, PyObject *name) Example_getattro(PyObject *op, PyObject *name)
{ {
ExampleObject *self = ExampleObject_CAST(op);
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
@ -87,8 +92,9 @@ Example_getattro(ExampleObject *self, PyObject *name)
} }
static int 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) { if (self->x_attr == NULL) {
self->x_attr = PyDict_New(); self->x_attr = PyDict_New();
if (self->x_attr == NULL) if (self->x_attr == NULL)
@ -212,7 +218,7 @@ PyDoc_STRVAR(_StateAccessType_decrement_count__doc__,
// Intentionally does not use Argument Clinic // Intentionally does not use Argument Clinic
static PyObject * static PyObject *
_StateAccessType_increment_count_noclinic(StateAccessTypeObject *self, _StateAccessType_increment_count_noclinic(PyObject *self,
PyTypeObject *defining_class, PyTypeObject *defining_class,
PyObject *const *args, PyObject *const *args,
Py_ssize_t nargs, Py_ssize_t nargs,