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 bytesiobuf
  • Loading branch information
picnixz committed Jan 20, 2025
commit fc3cd12689a19ef793a42eadc0706f3f24802989
22 changes: 14 additions & 8 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef struct {
bytesio *source;
} bytesiobuf;

#define _bytesiobuf_CAST(op) ((bytesiobuf *)(op))

/* The bytesio object can be in three states:
* Py_REFCNT(buf) == 1, exports == 0.
* Py_REFCNT(buf) > 1. exports == 0,
Expand Down Expand Up @@ -1075,9 +1077,10 @@ PyType_Spec bytesio_spec = {
*/

static int
bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
bytesiobuf_getbuffer(PyObject *op, Py_buffer *view, int flags)
{
bytesio *b = (bytesio *) obj->source;
bytesiobuf *obj = _bytesiobuf_CAST(op);
bytesio *b = _bytesio_CAST(obj->source);

if (view == NULL) {
PyErr_SetString(PyExc_BufferError,
Expand All @@ -1090,34 +1093,37 @@ bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
}

/* cannot fail if view != NULL and readonly == 0 */
(void)PyBuffer_FillInfo(view, (PyObject*)obj,
(void)PyBuffer_FillInfo(view, op,
PyBytes_AS_STRING(b->buf), b->string_size,
0, flags);
b->exports++;
return 0;
}

static void
bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
bytesiobuf_releasebuffer(PyObject *op, Py_buffer *Py_UNUSED(view))
{
bytesio *b = (bytesio *) obj->source;
bytesiobuf *obj = _bytesiobuf_CAST(op);
bytesio *b = _bytesio_CAST(obj->source);
b->exports--;
}

static int
bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
bytesiobuf_traverse(PyObject *op, visitproc visit, void *arg)
{
bytesiobuf *self = _bytesiobuf_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->source);
return 0;
}

static void
bytesiobuf_dealloc(bytesiobuf *self)
bytesiobuf_dealloc(PyObject *op)
{
bytesiobuf *self = _bytesiobuf_CAST(op);
PyTypeObject *tp = Py_TYPE(self);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
PyObject_GC_UnTrack(op);
Py_CLEAR(self->source);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down
0