8000 gh-111178: fix UBSan failures in `Modules/_io/*.c` by picnixz · Pull Request #129083 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111178: fix UBSan failures in Modules/_io/*.c #129083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 8, 2025
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
Prev Previous commit
Next Next commit
fix UBSan failures for iobase
  • Loading branch information
picnixz committed Jan 20, 2025
commit cd728f5e2ecb3ea3ada4151f7923e7f67ba0dff4
22 changes: 13 additions & 9 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
PyObject *dict;
PyObject *weakreflist;
} iobase;
#define _iobase_CAST(op) ((iobase *)(op))

PyDoc_STRVAR(iobase_doc,
"The abstract base class for all I/O classes.\n"
Expand Down Expand Up @@ -342,31 +343,34 @@ _PyIOBase_finalize(PyObject *self)
}

static int
iobase_traverse(iobase *self, visitproc visit, void *arg)
iobase_traverse(PyObject *op, visitproc visit, void *arg)
{
iobase *self = _iobase_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->dict);
return 0;
}

static int
iobase_clear(iobase *self)
iobase_clear(PyObject *op)
{
iobase *self = _iobase_CAST(op);
Py_CLEAR(self->dict);
return 0;
}

/* Destructor */

static void
iobase_dealloc(iobase *self)
iobase_dealloc(PyObject *self)
{
/* NOTE: since IOBaseObject has its own dict, Python-defined attributes
are still available here for close() to use.
However, if the derived class declares a __slots__, those slots are
already gone.
*/
if (_PyIOBase_finalize((PyObject *) self) < 0) {
iobase *base = _iobase_CAST(self);
if (_PyIOBase_finalize(self) < 0) {
/* When called from a heap type's dealloc, the type will be
decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
if (_PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) {
Expand All @@ -376,10 +380,10 @@ iobase_dealloc(iobase *self)
}
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
Py_CLEAR(self->dict);
tp->tp_free((PyObject *)self);
if (base->weakreflist != NULL)
PyObject_ClearWeakRefs(self);
Py_CLEAR(base->dict);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -852,7 +856,7 @@ static PyMethodDef iobase_methods[] = {

static PyGetSetDef iobase_getset[] = {
{"__dict__", PyObject_GenericGetDict, NULL, NULL},
{"closed", (getter)iobase_closed_get, NULL, NULL},
{"closed", iobase_closed_get, NULL, NULL},
{NULL}
};

Expand Down
0