Skip to content

Commit 28ffdc5

Browse files
authored
gh-111178: fix UBSan failures in Python/bltinmodule.c (GH-128235)
* fix UBSan failures for `filterobject` * fix UBSan failures for `mapobject` * fix UBSan failures for `zipobject`
1 parent 613240b commit 28ffdc5

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

Python/bltinmodule.c

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ typedef struct {
494494
PyObject *it;
495495
} filterobject;
496496

497+
#define _filterobject_CAST(op) ((filterobject *)(op))
498+
497499
static PyObject *
498500
filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
499501
{
@@ -559,8 +561,9 @@ filter_vectorcall(PyObject *type, PyObject * const*args,
559561
}
560562

561563
static void
562-
filter_dealloc(filterobject *lz)
564+
filter_dealloc(PyObject *self)
563565
{
566+
filterobject *lz = _filterobject_CAST(self);
564567
PyObject_GC_UnTrack(lz);
565568
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
566569
Py_XDECREF(lz->func);
@@ -570,16 +573,18 @@ filter_dealloc(filterobject *lz)
570573
}
571574

572575
static int
573-
filter_traverse(filterobject *lz, visitproc visit, void *arg)
576+
filter_traverse(PyObject *self, visitproc visit, void *arg)
574577
{
578+
filterobject *lz = _filterobject_CAST(self);
575579
Py_VISIT(lz->it);
576580
Py_VISIT(lz->func);
577581
return 0;
578582
}
579583

580584
static PyObject *
581-
filter_next(filterobject *lz)
585+
filter_next(PyObject *self)
582586
{
587+
filterobject *lz = _filterobject_CAST(self);
583588
PyObject *item;
584589
PyObject *it = lz->it;
585590
long ok;
@@ -613,15 +618,16 @@ filter_next(filterobject *lz)
613618
}
614619

615620
static PyObject *
616-
filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
621+
filter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
617622
{
623+
filterobject *lz = _filterobject_CAST(self);
618624
return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
619625
}
620626

621627
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
622628

623629
static PyMethodDef filter_methods[] = {
624-
{"__reduce__", _PyCFunction_CAST(filter_reduce), METH_NOARGS, reduce_doc},
630+
{"__reduce__", filter_reduce, METH_NOARGS, reduce_doc},
625631
{NULL, NULL} /* sentinel */
626632
};
627633

@@ -638,7 +644,7 @@ PyTypeObject PyFilter_Type = {
638644
sizeof(filterobject), /* tp_basicsize */
639645
0, /* tp_itemsize */
640646
/* methods */
641-
(destructor)filter_dealloc, /* tp_dealloc */
647+
filter_dealloc, /* tp_dealloc */
642648
0, /* tp_vectorcall_offset */
643649
0, /* tp_getattr */
644650
0, /* tp_setattr */
@@ -656,12 +662,12 @@ PyTypeObject PyFilter_Type = {
656662
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
657663
Py_TPFLAGS_BASETYPE, /* tp_flags */
658664
filter_doc, /* tp_doc */
659-
(traverseproc)filter_traverse, /* tp_traverse */
665+
filter_traverse, /* tp_traverse */
660666
0, /* tp_clear */
661667
0, /* tp_richcompare */
662668
0, /* tp_weaklistoffset */
663669
PyObject_SelfIter, /* tp_iter */
664-
(iternextfunc)filter_next, /* tp_iternext */
670+
filter_next, /* tp_iternext */
665671
filter_methods, /* tp_methods */
666672
0, /* tp_members */
667673
0, /* tp_getset */
@@ -674,7 +680,7 @@ PyTypeObject PyFilter_Type = {
674680
PyType_GenericAlloc, /* tp_alloc */
675681
filter_new, /* tp_new */
676682
PyObject_GC_Del, /* tp_free */
677-
.tp_vectorcall = (vectorcallfunc)filter_vectorcall
683+
.tp_vectorcall = filter_vectorcall
678684
};
679685

680686

@@ -1319,6 +1325,8 @@ typedef struct {
13191325
int strict;
13201326
} mapobject;
13211327

1328+
#define _mapobject_CAST(op) ((mapobject *)(op))
1329+
13221330
static PyObject *
13231331
map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13241332
{
@@ -1422,25 +1430,28 @@ map_vectorcall(PyObject *type, PyObject * const*args,
14221430
}
14231431

14241432
static void
1425-
map_dealloc(mapobject *lz)
1433+
map_dealloc(PyObject *self)
14261434
{
1435+
mapobject *lz = _mapobject_CAST(self);
14271436
PyObject_GC_UnTrack(lz);
14281437
Py_XDECREF(lz->iters);
14291438
Py_XDECREF(lz->func);
14301439
Py_TYPE(lz)->tp_free(lz);
14311440
}
14321441

14331442
static int
1434-
map_traverse(mapobject *lz, visitproc visit, void *arg)
1443+
map_traverse(PyObject *self, visitproc visit, void *arg)
14351444
{
1445+
mapobject *lz = _mapobject_CAST(self);
14361446
Py_VISIT(lz->iters);
14371447
Py_VISIT(lz->func);
14381448
return 0;
14391449
}
14401450

14411451
static PyObject *
1442-
map_next(mapobject *lz)
1452+
map_next(PyObject *self)
14431453
{
1454+
mapobject *lz = _mapobject_CAST(self);
14441455
Py_ssize_t i;
14451456
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
14461457
PyObject **stack;
@@ -1523,8 +1534,9 @@ map_next(mapobject *lz)
15231534
}
15241535

15251536
static PyObject *
1526-
map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
1537+
map_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
15271538
{
1539+
mapobject *lz = _mapobject_CAST(self);
15281540
Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
15291541
PyObject *args = PyTuple_New(numargs+1);
15301542
Py_ssize_t i;
@@ -1545,19 +1557,20 @@ map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
15451557
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
15461558

15471559
static PyObject *
1548-
map_setstate(mapobject *lz, PyObject *state)
1560+
map_setstate(PyObject *self, PyObject *state)
15491561
{
15501562
int strict = PyObject_IsTrue(state);
15511563
if (strict < 0) {
15521564
return NULL;
15531565
}
1566+
mapobject *lz = _mapobject_CAST(self);
15541567
lz->strict = strict;
15551568
Py_RETURN_NONE;
15561569
}
15571570

15581571
static PyMethodDef map_methods[] = {
1559-
{"__reduce__", _PyCFunction_CAST(map_reduce), METH_NOARGS, reduce_doc},
1560-
{"__setstate__", _PyCFunction_CAST(map_setstate), METH_O, setstate_doc},
1572+
{"__reduce__", map_reduce, METH_NOARGS, reduce_doc},
1573+
{"__setstate__", map_setstate, METH_O, setstate_doc},
15611574
{NULL, NULL} /* sentinel */
15621575
};
15631576

@@ -1578,7 +1591,7 @@ PyTypeObject PyMap_Type = {
15781591
sizeof(mapobject), /* tp_basicsize */
15791592
0, /* tp_itemsize */
15801593
/* methods */
1581-
(destructor)map_dealloc, /* tp_dealloc */
1594+
map_dealloc, /* tp_dealloc */
15821595
0, /* tp_vectorcall_offset */
15831596
0, /* tp_getattr */
15841597
0, /* tp_setattr */
@@ -1596,12 +1609,12 @@ PyTypeObject PyMap_Type = {
15961609
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
15971610
Py_TPFLAGS_BASETYPE, /* tp_flags */
15981611
map_doc, /* tp_doc */
1599-
(traverseproc)map_traverse, /* tp_traverse */
1612+
map_traverse, /* tp_traverse */
16001613
0, /* tp_clear */
16011614
0, /* tp_richcompare */
16021615
0, /* tp_weaklistoffset */
16031616
PyObject_SelfIter, /* tp_iter */
1604-
(iternextfunc)map_next, /* tp_iternext */
1617+
map_next, /* tp_iternext */
16051618
map_methods, /* tp_methods */
16061619
0, /* tp_members */
16071620
0, /* tp_getset */
@@ -1614,7 +1627,7 @@ PyTypeObject PyMap_Type = {
16141627
PyType_GenericAlloc, /* tp_alloc */
16151628
map_new, /* tp_new */
16161629
PyObject_GC_Del, /* tp_free */
1617-
.tp_vectorcall = (vectorcallfunc)map_vectorcall
1630+
.tp_vectorcall = map_vectorcall
16181631
};
16191632

16201633

@@ -2965,6 +2978,8 @@ typedef struct {
29652978
int strict;
29662979
} zipobject;
29672980

2981+
#define _zipobject_CAST(op) ((zipobject *)(op))
2982+
29682983
static PyObject *
29692984
zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
29702985
{
@@ -3033,25 +3048,29 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30333048
}
30343049

30353050
static void
3036-
zip_dealloc(zipobject *lz)
3051+
zip_dealloc(PyObject *self)
30373052
{
3053+
zipobject *lz = _zipobject_CAST(self);
30383054
PyObject_GC_UnTrack(lz);
30393055
Py_XDECREF(lz->ittuple);
30403056
Py_XDECREF(lz->result);
30413057
Py_TYPE(lz)->tp_free(lz);
30423058
}
30433059

30443060
static int
3045-
zip_traverse(zipobject *lz, visitproc visit, void *arg)
3061+
zip_traverse(PyObject *self, visitproc visit, void *arg)
30463062
{
3063+
zipobject *lz = _zipobject_CAST(self);
30473064
Py_VISIT(lz->ittuple);
30483065
Py_VISIT(lz->result);
30493066
return 0;
30503067
}
30513068

30523069
static PyObject *
3053-
zip_next(zipobject *lz)
3070+
zip_next(PyObject *self)
30543071
{
3072+
zipobject *lz = _zipobject_CAST(self);
3073+
30553074
Py_ssize_t i;
30563075
Py_ssize_t tuplesize = lz->tuplesize;
30573076
PyObject *result = lz->result;
@@ -3141,8 +3160,9 @@ zip_next(zipobject *lz)
31413160
}
31423161

31433162
static PyObject *
3144-
zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
3163+
zip_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
31453164
{
3165+
zipobject *lz = _zipobject_CAST(self);
31463166
/* Just recreate the zip with the internal iterator tuple */
31473167
if (lz->strict) {
31483168
return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True);
@@ -3151,19 +3171,20 @@ zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
31513171
}
31523172

31533173
static PyObject *
3154-
zip_setstate(zipobject *lz, PyObject *state)
3174+
zip_setstate(PyObject *self, PyObject *state)
31553175
{
31563176
int strict = PyObject_IsTrue(state);
31573177
if (strict < 0) {
31583178
return NULL;
31593179
}
3180+
zipobject *lz = _zipobject_CAST(self);
31603181
lz->strict = strict;
31613182
Py_RETURN_NONE;
31623183
}
31633184

31643185
static PyMethodDef zip_methods[] = {
3165-
{"__reduce__", _PyCFunction_CAST(zip_reduce), METH_NOARGS, reduce_doc},
3166-
{"__setstate__", _PyCFunction_CAST(zip_setstate), METH_O, setstate_doc},
3186+
{"__reduce__", zip_reduce, METH_NOARGS, reduce_doc},
3187+
{"__setstate__", zip_setstate, METH_O, setstate_doc},
31673188
{NULL} /* sentinel */
31683189
};
31693190

@@ -3188,7 +3209,7 @@ PyTypeObject PyZip_Type = {
31883209
sizeof(zipobject), /* tp_basicsize */
31893210
0, /* tp_itemsize */
31903211
/* methods */
3191-
(destructor)zip_dealloc, /* tp_dealloc */
3212+
zip_dealloc, /* tp_dealloc */
31923213
0, /* tp_vectorcall_offset */
31933214
0, /* tp_getattr */
31943215
0, /* tp_setattr */
@@ -3206,12 +3227,12 @@ PyTypeObject PyZip_Type = {
32063227
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
32073228
Py_TPFLAGS_BASETYPE, /* tp_flags */
32083229
zip_doc, /* tp_doc */
3209-
(traverseproc)zip_traverse, /* tp_traverse */
3230+
zip_traverse, /* tp_traverse */
32103231
0, /* tp_clear */
32113232
0, /* tp_richcompare */
32123233
0, /* tp_weaklistoffset */
32133234
PyObject_SelfIter, /* tp_iter */
3214-
(iternextfunc)zip_next, /* tp_iternext */
3235+
zip_next, /* tp_iternext */
32153236
zip_methods, /* tp_methods */
32163237
0, /* tp_members */
32173238
0, /* tp_getset */

0 commit comments

Comments
 (0)