8000 gh-111178: fix UBSan failures in `Modules/xx*.c` (GH-129797) · python/cpython@ead0913 · GitHub
[go: up one dir, main page]

Skip to content

Commit ead0913

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

File tree

4 files changed

+112
-77
lines changed

4 files changed

+112
-77
lines changed

Modules/xxlimited.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ typedef struct {
9090
Py_ssize_t x_exports; /* how many buffer are exported */
9191
} XxoObject;
9292

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

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

116117
static int
117-
Xxo_traverse(PyObject *self_obj, visitproc visit, void *arg)
118+
Xxo_traverse(PyObject *op, visitproc visit, void *arg)
118119
{
119120
// Visit the type
120-
Py_VISIT(Py_TYPE(self_obj));
121+
Py_VISIT(Py_TYPE(op));
121122

122123
// Visit the attribute dict
123-
XxoObject *self = (XxoObject *)self_obj;
124+
XxoObject *self = XxoObject_CAST(op);
124125
Py_VISIT(self->x_attr);
125126
return 0;
126127
}
127128

128129
static int
129-
Xxo_clear(XxoObject *self)
130+
Xxo_clear(PyObject *op)
130131
{
132+
XxoObject *self = XxoObject_CAST(op);
131133
Py_CLEAR(self->x_attr);
132134
return 0;
133135
}
134136

135137
static void
136-
Xxo_finalize(PyObject *self_obj)
138+
Xxo_finalize(PyObject *op)
137139
{
138-
XxoObject *self = (XxoObject *)self_obj;
140+
XxoObject *self = XxoObject_CAST(op);
139141
Py_CLEAR(self->x_attr);
140142
}
141143

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

156158
static PyObject *
157-
Xxo_getattro(XxoObject *self, PyObject *name)
159+
Xxo_getattro(PyObject *op, PyObject *name)
158160
{
161+
XxoObject *self = XxoObject_CAST(op);
159162
if (self->x_attr != NULL) {
160163
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
161164
if (v != NULL) {
@@ -165,12 +168,13 @@ Xxo_getattro(XxoObject *self, PyObject *name)
165168
return NULL;
166169
}
167170
}
168-
return PyObject_GenericGetAttr((PyObject *)self, name);
171+
return PyObject_GenericGetAttr(op, name);
169172
}
170173

171174
static int
172-
Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
175+
Xxo_setattro(PyObject *op, PyObject *name, PyObject *v)
173176
{
177+
XxoObject *self = XxoObject_CAST(op);
174178
if (self->x_attr == NULL) {
175179
// prepare the attribute dict
176180
self->x_attr = PyDict_New();
@@ -197,8 +201,8 @@ Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
197201
/* Xxo methods */
198202

199203
static PyObject *
200-
Xxo_demo(XxoObject *self, PyTypeObject *defining_class,
201-
PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
204+
Xxo_demo(PyObject *op, PyTypeObject *defining_class,
205+
PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
202206
{
203207
if (kwnames != NULL && PyObject_Length(kwnames)) {
204208
PyErr_SetString(PyExc_TypeError, "demo() takes no keyword arguments");
@@ -233,9 +237,10 @@ static PyMethodDef Xxo_methods[] = {
233237
/* Xxo buffer interface */
234238

235239
static int
236-
Xxo_getbuffer(XxoObject *self, Py_buffer *view, int flags)
240+
Xxo_getbuffer(PyObject *op, Py_buffer *view, int flags)
237241
{
238-
int res = PyBuffer_FillInfo(view, (PyObject*)self,
242+
XxoObject *self = XxoObject_CAST(op);
243+
int res = PyBuffer_FillInfo(view, op,
239244
(void *)self->x_buffer, BUFSIZE,
240245
0, flags);
241246
if (res == 0) {
@@ -245,14 +250,16 @@ Xxo_getbuffer(XxoObject *self, Py_buffer *view, int flags)
245250
}
246251

247252
static void
248-
Xxo_releasebuffer(XxoObject *self, Py_buffer *view)
253+
Xxo_releasebuffer(PyObject *op, Py_buffer *Py_UNUSED(view))
249254
{
255+
XxoObject *self = XxoObject_CAST(op);
250256
self->x_exports--;
251257
}
252258

253259
static PyObject *
254-
Xxo_get_x_exports(XxoObject *self, void *c)
260+
Xxo_get_x_exports(PyObject *op, void *Py_UNUSED(closure))
255261
{
262+
XxoObject *self = XxoObject_CAST(op);
256263
return PyLong_FromSsize_t(self->x_exports);
257264
}
258265

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

264271
static PyGetSetDef Xxo_getsetlist[] = {
265-
{"x_exports", (getter) Xxo_get_x_exports, NULL, NULL},
272+
{"x_exports", Xxo_get_x_exports, NULL, NULL},
266273
{NULL},
267274
};
268275

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
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,48 @@ typedef struct {
2525

2626
static PyTypeObject Xxo_Type;
2727

28-
#define XxoObject_Check(v) Py_IS_TYPE(v, &Xxo_Type)
28+
#define XxoObject_CAST(op) ((XxoObject *)(op))
29+
#define XxoObject_Check(v) Py_IS_TYPE(v, &Xxo_Type)
2930

3031
static XxoObject *
3132
newXxoObject(PyObject *arg)
3233
{
33-
XxoObject *self;
34-
self = PyObject_New(XxoObject, &Xxo_Type);
35-
if (self == NULL)
34+
XxoObject *self = PyObject_New(XxoObject, &Xxo_Type);
35+
if (self == NULL) {
3636
return NULL;
37+
}
3738
self->x_attr = NULL;
3839
return self;
3940
}
4041

4142
/* Xxo methods */
4243

4344
static void
44-
Xxo_dealloc(XxoObject *self)
45+
Xxo_dealloc(PyObject *op)
4546
{
47+
XxoObject *self = XxoObject_CAST(op);
4648
Py_XDECREF(self->x_attr);
4749
PyObject_Free(self);
4850
}
4951

5052
static PyObject *
51-
Xxo_demo(XxoObject *self, PyObject *args)
53+
Xxo_demo(PyObject *Py_UNUSED(op), PyObject *args)
5254
{
53-
if (!PyArg_ParseTuple(args, ":demo"))
55+
if (!PyArg_ParseTuple(args, ":demo")) {
5456
return NULL;
57+
}
5558
return Py_NewRef(Py_None);
5659
}
5760

5861
static PyMethodDef Xxo_methods[] = {
59-
{"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
60-
PyDoc_STR("demo() -> None")},
61-
{NULL, NULL} /* sentinel */
62+
{"demo", Xxo_demo, METH_VARARGS, PyDoc_STR("demo() -> None")},
63+
{NULL, NULL} /* sentinel */
6264
};
6365

6466
static PyObject *
65-
Xxo_getattro(XxoObject *self, PyObject *name)
67+
Xxo_getattro(PyObject *op, PyObject *name)
6668
{
69+
XxoObject *self = XxoObject_CAST(op);
6770
if (self->x_attr != NULL) {
6871
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
6972
if (v != NULL) {
@@ -73,26 +76,28 @@ Xxo_getattro(XxoObject *self, PyObject *name)
7376
return NULL;
7477
}
7578
}
76-
return PyObject_GenericGetAttr((PyObject *)self, name);
79+
return PyObject_GenericGetAttr(op, name);
7780
}
7881

7982
static int
80-
Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
83+
Xxo_setattr(PyObject *op, const char *name, PyObject *v)
8184
{
85+
XxoObject *self = XxoObject_CAST(op);
8286
if (self->x_attr == NULL) {
8387
self->x_attr = PyDict_New();
84-
if (self->x_attr == NULL)
88+
if (self->x_attr == NULL) {
8589
return -1;
90+
}
8691
}
8792
if (v == NULL) {
8893
int rv = PyDict_DelItemString(self->x_attr, name);
89-
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
94+
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
9095
PyErr_SetString(PyExc_AttributeError,
91-
"delete non-existing Xxo attribute");
96+
"delete non-existing Xxo attribute");
97+
}
9298
return rv;
9399
}
94-
else
95-
return PyDict_SetItemString(self->x_attr, name, v);
100+
return PyDict_SetItemString(self->x_attr, name, v);
96101
}
97102

98103
static PyTypeObject Xxo_Type = {
@@ -103,10 +108,10 @@ static PyTypeObject Xxo_Type = {
103108
sizeof(XxoObject), /*tp_basicsize*/
104109
0, /*tp_itemsize*/
105110
/* methods */
106-
(destructor)Xxo_dealloc, /*tp_dealloc*/
111+
Xxo_dealloc, /*tp_dealloc*/
107112
0, /*tp_vectorcall_offset*/
108-
(getattrfunc)0, /*tp_getattr*/
109-
(setattrfunc)Xxo_setattr, /*tp_setattr*/
113+
0, /*tp_getattr*/
114+
Xxo_setattr, /*tp_setattr*/
110115
0, /*tp_as_async*/
111116
0, /*tp_repr*/
112117
0, /*tp_as_number*/
@@ -115,7 +120,7 @@ static PyTypeObject Xxo_Type = {
115120
0, /*tp_hash*/
116121
0, /*tp_call*/
117122
0, /*tp_str*/
118-
(getattrofunc)Xxo_getattro, /*tp_getattro*/
123+
Xxo_getattro, /*tp_getattro*/
119124
0, /*tp_setattro*/
120125
0, /*tp_as_buffer*/
121126
Py_TPFLAGS_DEFAULT, /*tp_flags*/

0 commit comments

Comments
 (0)
0