From b7520f1706d003b43c041e9e6772a93511f11369 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 18 Nov 2024 23:19:08 +0300 Subject: [PATCH 1/2] gh-126980: Fix `bytearray.__buffer__` crash on `PyBUF_{READ,WRITE}` --- Lib/test/test_buffer.py | 7 +++++++ .../2024-11-18-23-18-17.gh-issue-126980.r8QHdi.rst | 3 +++ Objects/bytearrayobject.c | 5 +++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-23-18-17.gh-issue-126980.r8QHdi.rst diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index cb38a69e390f3a..9a43b636f889a2 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -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 diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-23-18-17.gh-issue-126980.r8QHdi.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-23-18-17.gh-issue-126980.r8QHdi.rst new file mode 100644 index 00000000000000..84484e7c3001da --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-23-18-17.gh-issue-126980.r8QHdi.rst @@ -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. diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 5a52b2f702ad0b..871f99b6f885ba 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -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; } From 99c2ed30ca96ee1d33b24264cb24756267fd5b1d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 19 Nov 2024 15:17:44 +0300 Subject: [PATCH 2/2] Update Lib/test/test_buffer.py Co-authored-by: Victor Stinner --- Lib/test/test_buffer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index 9a43b636f889a2..6ea02eb63fa57c 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -4441,10 +4441,11 @@ def test_issue_7385(self): def test_bytearray_release_buffer_read_flag(self): # See https://github.com/python/cpython/issues/126980 + obj = bytearray(b'abc') with self.assertRaises(SystemError): - bytearray().__buffer__(inspect.BufferFlags.READ) + obj.__buffer__(inspect.BufferFlags.READ) with self.assertRaises(SystemError): - bytearray().__buffer__(inspect.BufferFlags.WRITE) + obj.__buffer__(inspect.BufferFlags.WRITE) @support.cpython_only def test_pybuffer_size_from_format(self):