Skip to content

Commit af29d5c

Browse files
authored
gh-111178: fix UBSan failures for Modules/_testcapimodule.c (#131614)
Fix UBSan failures for various classes in `Modules/_testcapimodule.c`, remove some redundant casts and add some `Py_UNUSED()` usages.
1 parent 491b814 commit af29d5c

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

Modules/_testcapimodule.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ get_testerror(PyObject *self) {
5252
return state->error;
5353
}
5454

55+
static void
56+
simple_object_dealloc(PyObject *self)
57+
{
58+
PyObject_Free(self);
59+
}
60+
5561
/* Raise _testcapi.error with test_name + ": " + msg, and return NULL. */
5662

5763
static PyObject *
@@ -171,7 +177,7 @@ static PyTypeObject _HashInheritanceTester_Type = {
171177
"hashinheritancetester", /* Name of this type */
172178
sizeof(PyObject), /* Basic object size */
173179
0, /* Item size for varobject */
174-
(destructor)PyObject_Free, /* tp_dealloc */
180+
simple_object_dealloc, /* tp_dealloc */
175181
0, /* tp_vectorcall_offset */
176182
0, /* tp_getattr */
177183
0, /* tp_setattr */
@@ -1737,7 +1743,7 @@ meth_o(PyObject* self, PyObject* obj)
17371743
}
17381744

17391745
static PyObject*
1740-
meth_noargs(PyObject* self, PyObject* ignored)
1746+
meth_noargs(PyObject* self, PyObject *Py_UNUSED(dummy))
17411747
{
17421748
return _null_to_none(self);
17431749
}
@@ -2552,10 +2558,10 @@ static PyMethodDef TestMethods[] = {
25522558
{"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
25532559
{"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS},
25542560
{"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
2555-
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
2556-
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
2561+
{"test_capsule", test_capsule, METH_NOARGS},
2562+
{"test_from_contiguous", test_from_contiguous, METH_NOARGS},
25572563
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
2558-
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
2564+
{"test_pep3118_obsolete_write_locks", test_pep3118_obsolete_write_locks, METH_NOARGS},
25592565
#endif
25602566
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
25612567
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS},
@@ -2768,6 +2774,7 @@ typedef struct {
27682774
PyObject *ao_iterator;
27692775
} awaitObject;
27702776

2777+
#define awaitObject_CAST(op) ((awaitObject *)(op))
27712778

27722779
static PyObject *
27732780
awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@@ -2790,21 +2797,23 @@ awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
27902797

27912798

27922799
static void
2793-
awaitObject_dealloc(awaitObject *ao)
2800+
awaitObject_dealloc(PyObject *op)
27942801
{
2802+
awaitObject *ao = awaitObject_CAST(op);
27952803
Py_CLEAR(ao->ao_iterator);
27962804
Py_TYPE(ao)->tp_free(ao);
27972805
}
27982806

27992807

28002808
static PyObject *
2801-
awaitObject_await(awaitObject *ao)
2809+
awaitObject_await(PyObject *op)
28022810
{
2811+
awaitObject *ao = awaitObject_CAST(op);
28032812
return Py_NewRef(ao->ao_iterator);
28042813
}
28052814

28062815
static PyAsyncMethods awaitType_as_async = {
2807-
(unaryfunc)awaitObject_await, /* am_await */
2816+
awaitObject_await, /* am_await */
28082817
0, /* am_aiter */
28092818
0, /* am_anext */
28102819
0, /* am_send */
@@ -2816,7 +2825,7 @@ static PyTypeObject awaitType = {
28162825
"awaitType",
28172826
sizeof(awaitObject), /* tp_basicsize */
28182827
0, /* tp_itemsize */
2819-
(destructor)awaitObject_dealloc, /* destructor tp_dealloc */
2828+
awaitObject_dealloc, /* tp_dealloc */
28202829
0, /* tp_vectorcall_offset */
28212830
0, /* tp_getattr */
28222831
0, /* tp_setattr */
@@ -2871,8 +2880,9 @@ MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
28712880
}
28722881

28732882
void
2874-
MyList_dealloc(MyListObject* op)
2883+
MyList_dealloc(PyObject *self)
28752884
{
2885+
MyListObject *op = (MyListObject *)self;
28762886
if (op->deallocated) {
28772887
/* We cannot raise exceptions here but we still want the testsuite
28782888
* to fail when we hit this */
@@ -2887,7 +2897,7 @@ static PyTypeObject MyList_Type = {
28872897
"MyList",
28882898
sizeof(MyListObject),
28892899
0,
2890-
(destructor)MyList_dealloc, /* tp_dealloc */
2900+
MyList_dealloc, /* tp_dealloc */
28912901
0, /* tp_vectorcall_offset */
28922902
0, /* tp_getattr */
28932903
0, /* tp_setattr */
@@ -2935,11 +2945,11 @@ generic_alias_dealloc(PyObject *op)
29352945
{
29362946
PyGenericAliasObject *self = (PyGenericAliasObject*)op;
29372947
Py_CLEAR(self->item);
2938-
Py_TYPE(self)->tp_free((PyObject *)self);
2948+
Py_TYPE(self)->tp_free(self);
29392949
}
29402950

29412951
static PyObject *
2942-
generic_alias_mro_entries(PyObject *op, PyObject *bases)
2952+
generic_alias_mro_entries(PyObject *op, PyObject *Py_UNUSED(bases))
29432953
{
29442954
PyGenericAliasObject *self = (PyGenericAliasObject*)op;
29452955
return PyTuple_Pack(1, self->item);
@@ -3090,7 +3100,7 @@ ContainerNoGC_dealloc(PyObject *op)
30903100
{
30913101
ContainerNoGCobject *self = (ContainerNoGCobject*)op;
30923102
Py_DECREF(self->value);
3093-
Py_TYPE(self)->tp_free((PyObject *)self);
3103+
Py_TYPE(self)->tp_free(self);
30943104
}
30953105

30963106
static PyMemberDef ContainerNoGC_members[] = {

0 commit comments

Comments
 (0)