diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-12-14 10:04:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-14 10:04:23 (GMT) |
commit | bb36f72efcc6a656e0907ffa83620a1e44044895 (patch) | |
tree | f23bc442613ac1f8c869fdeecc4bb62843805fc1 /Modules/_io | |
parent | 4d5d9acb22ee8c6908ebd4fdc3d17472f8b286e3 (diff) | |
download | cpython-bb36f72efcc6a656e0907ffa83620a1e44044895.zip cpython-bb36f72efcc6a656e0907ffa83620a1e44044895.tar.gz cpython-bb36f72efcc6a656e0907ffa83620a1e44044895.tar.bz2 |
gh-111049: Fix crash during garbage collection of the BytesIO buffer object (GH-111221)
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/bytesio.c | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 16b8ac6..4a15c8e 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -990,7 +990,9 @@ static int bytesio_clear(bytesio *self) { Py_CLEAR(self->dict); - Py_CLEAR(self->buf); + if (self->exports == 0) { + Py_CLEAR(self->buf); + } return 0; } @@ -1096,13 +1098,6 @@ bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view) } static int -bytesiobuf_clear(bytesiobuf *self) -{ - Py_CLEAR(self->source); - return 0; -} - -static int bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg) { Py_VISIT(Py_TYPE(self)); @@ -1116,7 +1111,7 @@ bytesiobuf_dealloc(bytesiobuf *self) PyTypeObject *tp = Py_TYPE(self); /* bpo-31095: UnTrack is needed before calling any callbacks */ PyObject_GC_UnTrack(self); - (void)bytesiobuf_clear(self); + Py_CLEAR(self->source); tp->tp_free(self); Py_DECREF(tp); } @@ -1124,7 +1119,6 @@ bytesiobuf_dealloc(bytesiobuf *self) static PyType_Slot bytesiobuf_slots[] = { {Py_tp_dealloc, bytesiobuf_dealloc}, {Py_tp_traverse, bytesiobuf_traverse}, - {Py_tp_clear, bytesiobuf_clear}, // Buffer protocol {Py_bf_getbuffer, bytesiobuf_getbuffer}, |