8000 fix UBSan failures for `PyStopIterationObject` · python/cpython@3f1196b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f1196b

Browse files
committed
fix UBSan failures for PyStopIterationObject
1 parent 2683a25 commit 3f1196b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

Objects/exceptions.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,22 @@ static PyMemberDef StopIteration_members[] = {
626626
{NULL} /* Sentinel */
627627
};
628628

629+
static inline PyStopIterationObject *
630+
_PyStopIteration_CAST(PyObject *self)
631+
{
632+
assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_StopIteration));
633+
return (PyStopIterationObject *)self;
634+
}
635+
629636
static int
630-
StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
637+
StopIteration_init(PyObject *op, PyObject *args, PyObject *kwds)
631638
{
632639
Py_ssize_t size = PyTuple_GET_SIZE(args);
633640
PyObject *value;
634641

635-
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
642+
if (BaseException_init(op, args, kwds) == -1)
636643
return -1;
644+
PyStopIterationObject *self = _PyStopIteration_CAST(op);
637645
Py_CLEAR(self->value);
638646
if (size > 0)
639647
value = PyTuple_GET_ITEM(args, 0);
@@ -644,25 +652,27 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
644652
}
645653

646654
static int
647-
StopIteration_clear(PyStopIterationObject *self)
655+
StopIteration_clear(PyObject *op)
648656
{
657+
PyStopIterationObject *self = _PyStopIteration_CAST(op);
649658
Py_CLEAR(self->value);
650-
return BaseException_clear((PyBaseExceptionObject *)self);
659+
return BaseException_clear(op);
651660
}
652661

653662
static void
654-
StopIteration_dealloc(PyStopIterationObject *self)
663+
StopIteration_dealloc(PyObject *self)
655664
{
656665
PyObject_GC_UnTrack(self);
657-
StopIteration_clear(self);
658-
Py_TYPE(self)->tp_free((PyObject *)self);
666+
(void)StopIteration_clear(self);
667+
Py_TYPE(self)->tp_free(self);
659668
}
660669

661670
static int
662-
StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
671+
StopIteration_traverse(PyObject *op, visitproc visit, void *arg)
663672
{
673+
PyStopIterationObject *self = _PyStopIteration_CAST(op);
664674
Py_VISIT(self->value);
665-
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
675+
return BaseException_traverse(op, visit, arg);
666676
}
667677

668678
ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration,

0 commit comments

Comments
 (0)
0