Skip to content

Commit 6877987

Browse files
projectgusdpgeorge
authored andcommitted
tests/cpydiff: Add a note about risk of resizing memoryview targets.
This a stop-gap until there is a proper fix for this. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 4bed614 commit 6877987

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
categories: Types,memoryview
3+
description: memoryview can become invalid if its target is resized
4+
cause: CPython prevents a ``bytearray`` or ``io.bytesIO`` object from changing size while there is a ``memoryview`` object that references it. MicroPython requires the programmer to manually ensure that an object is not resized while any ``memoryview`` references it.
5+
6+
In the worst case scenario, resizing an object which is the target of a memoryview can cause the memoryview(s) to reference invalid freed memory (a use-after-free bug) and corrupt the MicroPython runtime.
7+
workaround: Do not change the size of any ``bytearray`` or ``io.bytesIO`` object that has a ``memoryview`` assigned to it.
8+
"""
9+
b = bytearray(b"abcdefg")
10+
m = memoryview(b)
11+
b.extend(b"hijklmnop")
12+
print(b, bytes(m))

0 commit comments

Comments
 (0)