gh-111178: fix UBSan failures in `Modules/xx*.c` by picnixz · Pull Request #129797 · python/cpython · GitHub
Skip to content
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
39 changes: 23 additions & 16 deletions Modules/xxlimited.c
41 changes: 24 additions & 17 deletions Modules/xxlimited_35.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ typedef struct {

static PyObject *Xxo_Type;

#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
#define XxoObject_CAST(op) ((XxoObject *)(op))
#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)

static XxoObject *
newXxoObject(PyObject *arg)
Expand All @@ -40,32 +41,36 @@ newXxoObject(PyObject *arg)
/* Xxo methods */

static int
Xxo_traverse(XxoObject *self, visitproc visit, void *arg)
Xxo_traverse(PyObject *op, visitproc visit, void *arg)
{
XxoObject *self = XxoObject_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->x_attr);
return 0;
}

static int
Xxo_clear(XxoObject *self)
Xxo_clear(PyObject *op)
{
XxoObject *self = XxoObject_CAST(op);
Py_CLEAR(self->x_attr);
return 0;
}

static void
Xxo_finalize(XxoObject *self)
Xxo_finalize(PyObject *op)
{
XxoObject *self = XxoObject_CAST(op);
Py_CLEAR(self->x_attr);
}

static PyObject *
Xxo_demo(XxoObject *self, PyObject *args)
Xxo_demo(PyObject *self, PyObject *args)
{
PyObject *o = NULL;
if (!PyArg_ParseTuple(args, "|O:demo", &o))
if (!PyArg_ParseTuple(args, "|O:demo", &o)) {
return NULL;
}
/* Test availability of fast type checks */
if (o != NULL && PyUnicode_Check(o)) {
return Py_NewRef(o);
Expand All @@ -74,14 +79,14 @@ Xxo_demo(XxoObject *self, PyObject *args)
}

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

static PyObject *
Xxo_getattro(XxoObject *self, PyObject *name)
Xxo_getattro(PyObject *op, PyObject *name)
{
XxoObject *self = XxoObject_CAST(op);
if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) {
Expand All @@ -91,26 +96,28 @@ Xxo_getattro(XxoObject *self, PyObject *name)
return NULL;
}
}
return PyObject_GenericGetAttr((PyObject *)self, name);
return PyObject_GenericGetAttr(op, name);
}

static int
Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
Xxo_setattr(PyObject *op, const char *name, PyObject *v)
{
XxoObject *self = XxoObject_CAST(op);
if (self->x_attr == NULL) {
self->x_attr = PyDict_New();
if (self->x_attr == NULL)
if (self->x_attr == NULL) {
return -1;
}
}
if (v == NULL) {
int rv = PyDict_DelItemString(self->x_attr, name);
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_SetString(PyExc_AttributeError,
"delete non-existing Xxo attribute");
"delete non-existing Xxo attribute");
}
return rv;
}
else
return PyDict_SetItemString(self->x_attr, name, v);
return PyDict_SetItemString(self->x_attr, name, v);
}

static PyType_Slot Xxo_Type_slots[] = {
Expand Down
49 changes: 27 additions & 22 deletions Modules/xxmodule.c
Loading