Skip to content

Commit 70306ce

Browse files
committed
fix UBSan failures for testcapimodule.c
1 parent be8e4d5 commit 70306ce

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

Modules/_testcapimodule.c

Lines changed: 29 additions & 15 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 PyObject *
56+
simple_object_destructor(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_destructor, /* 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},
@@ -2874,8 +2880,9 @@ MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
28742880
}
28752881

28762882
void
2877-
MyList_dealloc(MyListObject* op)
2883+
MyList_dealloc(PyObject *self)
28782884
{
2885+
MyListObject *op = (MyListObject *)self;
28792886
if (op->deallocated) {
28802887
/* We cannot raise exceptions here but we still want the testsuite
28812888
* to fail when we hit this */
@@ -2890,7 +2897,7 @@ static PyTypeObject MyList_Type = {
28902897
"MyList",
28912898
sizeof(MyListObject),
28922899
0,
2893-
(destructor)MyList_dealloc, /* tp_dealloc */
2900+
MyList_dealloc, /* tp_dealloc */
28942901
0, /* tp_vectorcall_offset */
28952902
0, /* tp_getattr */
28962903
0, /* tp_setattr */
@@ -2933,21 +2940,25 @@ typedef struct {
29332940
PyObject *item;
29342941
} PyGenericAliasObject;
29352942

2943+
#define PyGenericAliasObject_CAST(op) ((PyGenericAliasObject *)(op))
2944+
29362945
static void
2937-
generic_alias_dealloc(PyGenericAliasObject *self)
2946+
generic_alias_dealloc(PyObject *op)
29382947
{
2948+
PyGenericAliasObject *self = PyGenericAliasObject_CAST(op);
29392949
Py_CLEAR(self->item);
2940-
Py_TYPE(self)->tp_free((PyObject *)self);
2950+
Py_TYPE(self)->tp_free(self);
29412951
}
29422952

29432953
static PyObject *
2944-
generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
2954+
generic_alias_mro_entries(PyObject *op, PyObject *Py_UNUSED(bases))
29452955
{
2956+
PyGenericAliasObject *self = PyGenericAliasObject_CAST(op);
29462957
return PyTuple_Pack(1, self->item);
29472958
}
29482959

29492960
static PyMethodDef generic_alias_methods[] = {
2950-
{"__mro_entries__", _PyCFunction_CAST(generic_alias_mro_entries), METH_O, NULL},
2961+
{"__mro_entries__", generic_alias_mro_entries, METH_O, NULL},
29512962
{NULL} /* sentinel */
29522963
};
29532964

@@ -2956,7 +2967,7 @@ static PyTypeObject GenericAlias_Type = {
29562967
"GenericAlias",
29572968
sizeof(PyGenericAliasObject),
29582969
0,
2959-
.tp_dealloc = (destructor)generic_alias_dealloc,
2970+
.tp_dealloc = generic_alias_dealloc,
29602971
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
29612972
.tp_methods = generic_alias_methods,
29622973
};
@@ -3069,6 +3080,8 @@ typedef struct {
30693080
PyObject *value;
30703081
} ContainerNoGCobject;
30713082

3083+
#define ContainerNoGCobject_CAST(op) ((ContainerNoGCobject *)(op))
3084+
30723085
static PyObject *
30733086
ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
30743087
{
@@ -3087,10 +3100,11 @@ ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
30873100
}
30883101

30893102
static void
3090-
ContainerNoGC_dealloc(ContainerNoGCobject *self)
3103+
ContainerNoGC_dealloc(PyObject *op)
30913104
{
3105+
ContainerNoGCobject *self = ContainerNoGCobject_CAST(op);
30923106
Py_DECREF(self->value);
3093-
Py_TYPE(self)->tp_free((PyObject *)self);
3107+
Py_TYPE(self)->tp_free(self);
30943108
}
30953109

30963110
static PyMemberDef ContainerNoGC_members[] = {
@@ -3103,7 +3117,7 @@ static PyTypeObject ContainerNoGC_type = {
31033117
PyVarObject_HEAD_INIT(NULL, 0)
31043118
"_testcapi.ContainerNoGC",
31053119
sizeof(ContainerNoGCobject),
3106-
.tp_dealloc = (destructor)ContainerNoGC_dealloc,
3120+
.tp_dealloc = ContainerNoGC_dealloc,
31073121
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
31083122
.tp_members = ContainerNoGC_members,
31093123
.tp_new = ContainerNoGC_new,

0 commit comments

Comments
 (0)