gh-111178: fix UBSan failures in `Modules/xx*.c` (GH-129797) · donbarbos/cpython@ead0913 · GitHub
Skip to content

Commit ead0913

Browse files
authored
pythongh-111178: fix UBSan failures in Modules/xx*.c (pythonGH-129797)
Fix UBSan failures in `Modules/xxlimited.c`, `Modules/xxlimited_35.c`, `Modules/xxsubtype.c`, `Modules/xxmodule.c`
1 parent 3d40317 commit ead0913

4 files changed

Lines changed: 112 additions & 77 deletions

File tree

Modules/xxlimited.c

Lines changed: 23 additions & 16 deletions

Modules/xxlimited_35.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ typedef struct {
2424

2525
static PyObject *Xxo_Type;
2626

27-
#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
27+
#define XxoObject_CAST(op) ((XxoObject *)(op))
28+
#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
2829

2930
static XxoObject *
3031
newXxoObject(PyObject *arg)
@@ -40,32 +41,36 @@ newXxoObject(PyObject *arg)
4041
/* Xxo methods */
4142

4243
static int
43-
Xxo_traverse(XxoObject *self, visitproc visit, void *arg)
44+
Xxo_traverse(PyObject *op, visitproc visit, void *arg)
4445
{
46+
XxoObject *self = XxoObject_CAST(op);
4547
Py_VISIT(Py_TYPE(self));
4648
Py_VISIT(self->x_attr);
4749
return 0;
4850
}
4951

5052
static int
51-
Xxo_clear(XxoObject *self)
53+
Xxo_clear(PyObject *op)
5254
{
55+
XxoObject *self = XxoObject_CAST(op);
5356
Py_CLEAR(self->x_attr);
5457
return 0;
5558
}
5659

5760
static void
58-
Xxo_finalize(XxoObject *self)
61+
Xxo_finalize(PyObject *op)
5962
{
63+
XxoObject *self = XxoObject_CAST(op);
6064
Py_CLEAR(self->x_attr);
6165
}
6266

6367
static PyObject *
64-
Xxo_demo(XxoObject *self, PyObject *args)
68+
Xxo_demo(PyObject *self, PyObject *args)
6569
{
6670
PyObject *o = NULL;
67-
if (!PyArg_ParseTuple(args, "|O:demo", &o))
71+
if (!PyArg_ParseTuple(args, "|O:demo", &o)) {
6872
return NULL;
73+
}
6974
/* Test availability of fast type checks */
7075
if (o != NULL && PyUnicode_Check(o)) {
7176
return Py_NewRef(o);
@@ -74,14 +79,14 @@ Xxo_demo(XxoObject *self, PyObject *args)
7479
}
7580

7681
static PyMethodDef Xxo_methods[] = {
77-
{"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
78-
PyDoc_STR("demo() -> None")},
79-
{NULL, NULL} /* sentinel */
82+
{"demo", Xxo_demo, METH_VARARGS, PyDoc_STR("demo() -> None")},
83+
{NULL, NULL} /* sentinel */
8084
};
8185

8286
static PyObject *
83-
Xxo_getattro(XxoObject *self, PyObject *name)
87+
Xxo_getattro(PyObject *op, PyObject *name)
8488
{
89+
XxoObject *self = XxoObject_CAST(op);
8590
if (self->x_attr != NULL) {
8691
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
8792
if (v != NULL) {
@@ -91,26 +96,28 @@ Xxo_getattro(XxoObject *self, PyObject *name)
9196
return NULL;
9297
}
9398
}
94-
return PyObject_GenericGetAttr((PyObject *)self, name);
99+
return PyObject_GenericGetAttr(op, name);
95100
}
96101

97102
static int
98-
Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
103+
Xxo_setattr(PyObject *op, const char *name, PyObject *v)
99104
{
105+
XxoObject *self = XxoObject_CAST(op);
100106
if (self->x_attr == NULL) {
101107
self->x_attr = PyDict_New();
102-
if (self->x_attr == NULL)
108+
if (self->x_attr == NULL) {
103109
return -1;
110+
}
104111
}
105112
if (v == NULL) {
106113
int rv = PyDict_DelItemString(self->x_attr, name);
107-
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
114+
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
108115
PyErr_SetString(PyExc_AttributeError,
109-
"delete non-existing Xxo attribute");
116+
"delete non-existing Xxo attribute");
117+
}
110118
return rv;
111119
}
112-
else
113-
return PyDict_SetItemString(self->x_attr, name, v);
120+
return PyDict_SetItemString(self->x_attr, name, v);
114121
}
115122

116123
static PyType_Slot Xxo_Type_slots[] = {

Modules/xxmodule.c

Lines changed: 27 additions & 22 deletions

0 commit comments

Comments
 (0)