Skip to content
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

gh-111178: fix UBSan failures in Modules/xx*.c #129797

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef struct {
Py_ssize_t x_exports; /* how many buffer are exported */
} XxoObject;

#define XxoObject_CAST(op) ((XxoObject *)(op))
// XXX: no good way to do this yet
// #define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)

Expand All @@ -114,28 +115,29 @@ newXxoObject(PyObject *module)
/* Xxo finalization */

static int
Xxo_traverse(PyObject *self_obj, visitproc visit, void *arg)
Xxo_traverse(PyObject *op, visitproc visit, void *arg)
{
// Visit the type
Py_VISIT(Py_TYPE(self_obj));
Py_VISIT(Py_TYPE(op));

// Visit the attribute dict
XxoObject *self = (XxoObject *)self_obj;
XxoObject *self = XxoObject_CAST(op);
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(PyObject *self_obj)
Xxo_finalize(PyObject *op)
{
XxoObject *self = (XxoObject *)self_obj;
XxoObject *self = XxoObject_CAST(op);
Py_CLEAR(self->x_attr);
}

Expand All @@ -154,8 +156,9 @@ Xxo_dealloc(PyObject *self)
/* Xxo attribute handling */

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 @@ -165,12 +168,13 @@ Xxo_getattro(XxoObject *self, PyObject *name)
return NULL;
}
}
return PyObject_GenericGetAttr((PyObject *)self, name);
return PyObject_GenericGetAttr(op, name);
}

static int
Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
Xxo_setattro(PyObject *op, PyObject *name, PyObject *v)
{
XxoObject *self = XxoObject_CAST(op);
if (self->x_attr == NULL) {
// prepare the attribute dict
self->x_attr = PyDict_New();
Expand All @@ -197,8 +201,8 @@ Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
/* Xxo methods */

static PyObject *
Xxo_demo(XxoObject *self, PyTypeObject *defining_class,
PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Xxo_demo(PyObject *op, PyTypeObject *defining_class,
PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
if (kwnames != NULL && PyObject_Length(kwnames)) {
PyErr_SetString(PyExc_TypeError, "demo() takes no keyword arguments");
Expand Down Expand Up @@ -233,9 +237,10 @@ static PyMethodDef Xxo_methods[] = {
/* Xxo buffer interface */

static int
Xxo_getbuffer(XxoObject *self, Py_buffer *view, int flags)
Xxo_getbuffer(PyObject *op, Py_buffer *view, int flags)
{
int res = PyBuffer_FillInfo(view, (PyObject*)self,
XxoObject *self = XxoObject_CAST(op);
int res = PyBuffer_FillInfo(view, op,
(void *)self->x_buffer, BUFSIZE,
0, flags);
if (res == 0) {
Expand All @@ -245,14 +250,16 @@ Xxo_getbuffer(XxoObject *self, Py_buffer *view, int flags)
}

static void
Xxo_releasebuffer(XxoObject *self, Py_buffer *view)
Xxo_releasebuffer(PyObject *op, Py_buffer *Py_UNUSED(view))
{
XxoObject *self = XxoObject_CAST(op);
self->x_exports--;
}

static PyObject *
Xxo_get_x_exports(XxoObject *self, void *c)
Xxo_get_x_exports(PyObject *op, void *Py_UNUSED(closure))
{
XxoObject *self = XxoObject_CAST(op);
return PyLong_FromSsize_t(self->x_exports);
}

Expand All @@ -262,7 +269,7 @@ PyDoc_STRVAR(Xxo_doc,
"A class that explicitly stores attributes in an internal dict");

static PyGetSetDef Xxo_getsetlist[] = {
{"x_exports", (getter) Xxo_get_x_exports, NULL, NULL},
{"x_exports", Xxo_get_x_exports, NULL, NULL},
{NULL},
};

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,48 @@ typedef struct {

static PyTypeObject 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)
{
XxoObject *self;
self = PyObject_New(XxoObject, &Xxo_Type);
if (self == NULL)
XxoObject *self = PyObject_New(XxoObject, &Xxo_Type);
if (self == NULL) {
return NULL;
}
self->x_attr = NULL;
return self;
}

/* Xxo methods */

static void
Xxo_dealloc(XxoObject *self)
Xxo_dealloc(PyObject *op)
{
XxoObject *self = XxoObject_CAST(op);
Py_XDECREF(self->x_attr);
PyObject_Free(self);
}

static PyObject *
Xxo_demo(XxoObject *self, PyObject *args)
Xxo_demo(PyObject *Py_UNUSED(op), PyObject *args)
{
if (!PyArg_ParseTuple(args, ":demo"))
if (!PyArg_ParseTuple(args, ":demo")) {
return NULL;
}
return Py_NewRef(Py_None);
}

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 @@ -73,26 +76,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 PyTypeObject Xxo_Type = {
Expand All @@ -103,10 +108,10 @@ static PyTypeObject Xxo_Type = {
sizeof(XxoObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)Xxo_dealloc, /*tp_dealloc*/
Xxo_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
(getattrfunc)0, /*tp_getattr*/
(setattrfunc)Xxo_setattr, /*tp_setattr*/
0, /*tp_getattr*/
Xxo_setattr, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
Expand All @@ -115,7 +120,7 @@ static PyTypeObject Xxo_Type = {
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
(getattrofunc)Xxo_getattro, /*tp_getattro*/
Xxo_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
Expand Down
Loading
Loading