Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions Lib/test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,17 @@ def __bool__(self):
with self.assertRaises(ValueError):
m[MyIndex()]

# Other exceptions can be raised when working on a released buffer.
# See https://github.com/python/cpython/issues/142665.
ba = None
m = memoryview(bytearray(b'\xff'*size))
self.assertEqual(list(m[:MyIndex()]), [255] * 4)
with self.assertRaises(BufferError):
m[:MyIndex()]

ba = None
m = memoryview(bytearray(b'\xff'*size))
self.assertEqual(list(m[MyIndex():8]), [255] * 4)
with self.assertRaises(BufferError):
m[MyIndex():8]

ba = None
m = memoryview(bytearray(b'\xff'*size)).cast('B', (64, 2))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix use-after-free crashes when slicing a :class:`memoryview` or
accessing the elements of a sliced view concurrently mutates the
underlying buffer. Patch by Bénédikt Tran.
5 changes: 4 additions & 1 deletion Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,7 +2623,10 @@ memory_subscript(PyObject *_self, PyObject *key)
if (sliced == NULL)
return NULL;

if (init_slice(&sliced->view, key, 0) < 0) {
self->exports++;
int rc = init_slice(&sliced->view, key, 0);
self->exports--;
if (rc < 0) {
Py_DECREF(sliced);
return NULL;
}
Expand Down
Loading