Skip to content
Merged
Changes from 1 commit
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
Next Next commit
fix UBSan failures for bytesiterobject
  • Loading branch information
picnixz committed Dec 24, 2024
commit e7266039b644b1ac61ef07a3e9b8be7cee686b92
34 changes: 21 additions & 13 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2464,24 +2464,29 @@ typedef struct {
PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
} bytesiterobject;

#define _bytesiterobject_CAST(op) ((bytesiterobject *)(op))

static void
bytearrayiter_dealloc(bytesiterobject *it)
bytearrayiter_dealloc(PyObject *self)
{
bytesiterobject *it = _bytesiterobject_CAST(self);
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}

static int
bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
bytearrayiter_traverse(PyObject *self, visitproc visit, void *arg)
{
bytesiterobject *it = _bytesiterobject_CAST(self);
Py_VISIT(it->it_seq);
return 0;
}

static PyObject *
bytearrayiter_next(bytesiterobject *it)
bytearrayiter_next(PyObject *self)
{
bytesiterobject *it = _bytesiterobject_CAST(self);
PyByteArrayObject *seq;

assert(it != NULL);
Expand All @@ -2501,8 +2506,9 @@ bytearrayiter_next(bytesiterobject *it)
}

static PyObject *
bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
bytearrayiter_length_hint(PyObject *self, PyObject *Py_UNUSED(ignored))
{
bytesiterobject *it = _bytesiterobject_CAST(self);
Py_ssize_t len = 0;
if (it->it_seq) {
len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Expand All @@ -2517,14 +2523,14 @@ PyDoc_STRVAR(length_hint_doc,
"Private method returning an estimate of len(list(it)).");

static PyObject *
bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
bytearrayiter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));

/* _PyEval_GetBuiltin can invoke arbitrary code,
* call must be before access of iterator pointers.
* see issue #101765 */

bytesiterobject *it = _bytesiterobject_CAST(self);
if (it->it_seq != NULL) {
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
} else {
Expand All @@ -2533,11 +2539,13 @@ bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
}

static PyObject *
bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
bytearrayiter_setstate(PyObject *self, PyObject *state)
{
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;

bytesiterobject *it = _bytesiterobject_CAST(self);
if (it->it_seq != NULL) {
if (index < 0)
index = 0;
Expand All @@ -2551,11 +2559,11 @@ bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");

static PyMethodDef bytearrayiter_methods[] = {
{"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
{"__length_hint__", bytearrayiter_length_hint, METH_NOARGS,
length_hint_doc},
{"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
{"__reduce__", bytearrayiter_reduce, METH_NOARGS,
bytearray_reduce__doc__},
{"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
{"__setstate__", bytearrayiter_setstate, METH_O,
setstate_doc},
{NULL, NULL} /* sentinel */
};
Expand All @@ -2566,7 +2574,7 @@ PyTypeObject PyByteArrayIter_Type = {
sizeof(bytesiterobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)bytearrayiter_dealloc, /* tp_dealloc */
bytearrayiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand All @@ -2583,12 +2591,12 @@ PyTypeObject PyByteArrayIter_Type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
(traverseproc)bytearrayiter_traverse, /* tp_traverse */
bytearrayiter_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)bytearrayiter_next, /* tp_iternext */
bytearrayiter_next, /* tp_iternext */
bytearrayiter_methods, /* tp_methods */
0,
};
Expand Down