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.
This commit is contained in:
Bénédikt Tran 2025-03-24 11:14:22 +01:00 committed by GitHub
parent 491b8141f5
commit af29d5cfd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -52,6 +52,12 @@ get_testerror(PyObject *self) {
return state->error;
}
static void
simple_object_dealloc(PyObject *self)
{
PyObject_Free(self);
}
/* Raise _testcapi.error with test_name + ": " + msg, and return NULL. */
static PyObject *
@ -171,7 +177,7 @@ static PyTypeObject _HashInheritanceTester_Type = {
"hashinheritancetester", /* Name of this type */
sizeof(PyObject), /* Basic object size */
0, /* Item size for varobject */
(destructor)PyObject_Free, /* tp_dealloc */
simple_object_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
@ -1737,7 +1743,7 @@ meth_o(PyObject* self, PyObject* obj)
}
static PyObject*
meth_noargs(PyObject* self, PyObject* ignored)
meth_noargs(PyObject* self, PyObject *Py_UNUSED(dummy))
{
return _null_to_none(self);
}
@ -2552,10 +2558,10 @@ static PyMethodDef TestMethods[] = {
{"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
{"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS},
{"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
{"test_capsule", test_capsule, METH_NOARGS},
{"test_from_contiguous", test_from_contiguous, METH_NOARGS},
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
{"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
{"test_pep3118_obsolete_write_locks", test_pep3118_obsolete_write_locks, METH_NOARGS},
#endif
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS},
@ -2768,6 +2774,7 @@ typedef struct {
PyObject *ao_iterator;
} awaitObject;
#define awaitObject_CAST(op) ((awaitObject *)(op))
static PyObject *
awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@ -2790,21 +2797,23 @@ awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static void
awaitObject_dealloc(awaitObject *ao)
awaitObject_dealloc(PyObject *op)
{
awaitObject *ao = awaitObject_CAST(op);
Py_CLEAR(ao->ao_iterator);
Py_TYPE(ao)->tp_free(ao);
}
static PyObject *
awaitObject_await(awaitObject *ao)
awaitObject_await(PyObject *op)
{
awaitObject *ao = awaitObject_CAST(op);
return Py_NewRef(ao->ao_iterator);
}
static PyAsyncMethods awaitType_as_async = {
(unaryfunc)awaitObject_await, /* am_await */
awaitObject_await, /* am_await */
0, /* am_aiter */
0, /* am_anext */
0, /* am_send */
@ -2816,7 +2825,7 @@ static PyTypeObject awaitType = {
"awaitType",
sizeof(awaitObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)awaitObject_dealloc, /* destructor tp_dealloc */
awaitObject_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
@ -2871,8 +2880,9 @@ MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
void
MyList_dealloc(MyListObject* op)
MyList_dealloc(PyObject *self)
{
MyListObject *op = (MyListObject *)self;
if (op->deallocated) {
/* We cannot raise exceptions here but we still want the testsuite
* to fail when we hit this */
@ -2887,7 +2897,7 @@ static PyTypeObject MyList_Type = {
"MyList",
sizeof(MyListObject),
0,
(destructor)MyList_dealloc, /* tp_dealloc */
MyList_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
@ -2935,11 +2945,11 @@ generic_alias_dealloc(PyObject *op)
{
PyGenericAliasObject *self = (PyGenericAliasObject*)op;
Py_CLEAR(self->item);
Py_TYPE(self)->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free(self);
}
static PyObject *
generic_alias_mro_entries(PyObject *op, PyObject *bases)
generic_alias_mro_entries(PyObject *op, PyObject *Py_UNUSED(bases))
{
PyGenericAliasObject *self = (PyGenericAliasObject*)op;
return PyTuple_Pack(1, self->item);
@ -3090,7 +3100,7 @@ ContainerNoGC_dealloc(PyObject *op)
{
ContainerNoGCobject *self = (ContainerNoGCobject*)op;
Py_DECREF(self->value);
Py_TYPE(self)->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free(self);
}
static PyMemberDef ContainerNoGC_members[] = {