Skip to content

Commit

Permalink
pythongh-126980: Fix bytearray.__buffer__ crash on `PyBUF_{READ,WRI…
Browse files Browse the repository at this point in the history
…TE}`
  • Loading branch information
sobolevn committed Nov 18, 2024
1 parent b0fcc2c commit b7520f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4439,6 +4439,13 @@ def test_issue_7385(self):
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
self.assertRaises(BufferError, memoryview, x)

def test_bytearray_release_buffer_read_flag(self):
# See https://github.com/python/cpython/issues/126980
with self.assertRaises(SystemError):
bytearray().__buffer__(inspect.BufferFlags.READ)
with self.assertRaises(SystemError):
bytearray().__buffer__(inspect.BufferFlags.WRITE)

@support.cpython_only
def test_pybuffer_size_from_format(self):
# basic tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`~object.__buffer__` of :class:`bytearray` crashing when
:attr:`~inspect.BufferFlags.READ` or :attr:`~inspect.BufferFlags.WRITE` are
passed as flags.
5 changes: 3 additions & 2 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ bytearray_getbuffer(PyObject *self, Py_buffer *view, int flags)
}

void *ptr = (void *) PyByteArray_AS_STRING(obj);
/* cannot fail if view != NULL and readonly == 0 */
(void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
if (PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags) < 0) {
return -1;
}
obj->ob_exports++;
return 0;
}
Expand Down

0 comments on commit b7520f1

Please sign in to comment.