Skip to content

Commit c27a965

Browse files
committed
fix UBSan failures for lru_cache_object
1 parent 7b9170d commit c27a965

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

Modules/_functoolsmodule.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,8 @@ typedef struct lru_cache_object {
11071107
PyObject *weakreflist;
11081108
} lru_cache_object;
11091109

1110+
#define _lru_cache_object_CAST(op) ((lru_cache_object *)(op))
1111+
11101112
static PyObject *
11111113
lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
11121114
PyObject *kwds, int typed)
@@ -1542,8 +1544,9 @@ lru_cache_clear_list(lru_list_elem *link)
15421544
}
15431545

15441546
static int
1545-
lru_cache_tp_clear(lru_cache_object *self)
1547+
lru_cache_tp_clear(PyObject *op)
15461548
{
1549+
lru_cache_object *self = _lru_cache_object_CAST(op);
15471550
lru_list_elem *list = lru_cache_unlink_list(self);
15481551
Py_CLEAR(self->cache);
15491552
Py_CLEAR(self->func);
@@ -1556,23 +1559,25 @@ lru_cache_tp_clear(lru_cache_object *self)
15561559
}
15571560

15581561
static void
1559-
lru_cache_dealloc(lru_cache_object *obj)
1562+
lru_cache_dealloc(PyObject *op)
15601563
{
1564+
lru_cache_object *obj = _lru_cache_object_CAST(op);
15611565
PyTypeObject *tp = Py_TYPE(obj);
15621566
/* bpo-31095: UnTrack is needed before calling any callbacks */
15631567
PyObject_GC_UnTrack(obj);
15641568
if (obj->weakreflist != NULL) {
1565-
PyObject_ClearWeakRefs((PyObject*)obj);
1569+
PyObject_ClearWeakRefs(op);
15661570
}
15671571

1568-
(void)lru_cache_tp_clear(obj);
1572+
(void)lru_cache_tp_clear(op);
15691573
tp->tp_free(obj);
15701574
Py_DECREF(tp);
15711575
}
15721576

15731577
static PyObject *
1574-
lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds)
1578+
lru_cache_call(PyObject *op, PyObject *args, PyObject *kwds)
15751579
{
1580+
lru_cache_object *self = _lru_cache_object_CAST(op);
15761581
PyObject *result;
15771582
Py_BEGIN_CRITICAL_SECTION(self);
15781583
result = self->wrapper(self, args, kwds);
@@ -1649,8 +1654,9 @@ lru_cache_deepcopy(PyObject *self, PyObject *unused)
16491654
}
16501655

16511656
static int
1652-
lru_cache_tp_traverse(lru_cache_object *self, visitproc visit, void *arg)
1657+
lru_cache_tp_traverse(PyObject *op, visitproc visit, void *arg)
16531658
{
1659+
lru_cache_object *self = _lru_cache_object_CAST(op);
16541660
Py_VISIT(Py_TYPE(self));
16551661
lru_list_elem *link = self->root.next;
16561662
while (link != &self->root) {

0 commit comments

Comments
 (0)