Skip to content

gh-111178: fix UBSan failures for Modules/_testcapimodule.c #131614

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

Merged
merged 6 commits into from
Mar 24, 2025
Merged
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
38 changes: 24 additions & 14 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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)
Expand All @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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[] = {
Expand Down
Loading