← index #6523PR #9028
Related · high · value 0.738
QUERY · ISSUE

Taking slice of memoryview in Viper function crashes PyBoard

openby twisteroidambassadoropened 2020-10-07updated 2025-07-02
bugpy-core

On a PyBoard v1.1 with firmware 1.13, put the following code into a file:

import micropython


@micropython.viper
def take_memoryview_slice():
    ba = bytearray(10)
    mv = memoryview(ba)
    slice = mv[0:1]

Running take_memoryview_slice() resets the PyBoard.

CANDIDATE · PULL REQUEST

py/objarray: Raise error on out-of-bound memoryview slice start.

mergedby andrewleechopened 2022-08-07updated 2023-06-06
py-core

32 bit platforms only support a slice offset start of 24 bit max due to the limited size of https://github.com/micropython/micropython/blob/9dfabcd6d3d080aced888e8474e921f11dc979bb/py/objarray.h#L47

As it stands, if a memoryview object is sliced with a start index of 16MB or more then it's aliased back to the start of the array providing incorrect results.

I initially tried to increase the size of size_t free in the link above when used on a 32bit platform, however this breaks a lot more. Not sure why, presuably the total size of that structure is hardcoded / assumed elsewhere perhaps?

For now, this PR just raises an exception if the start index is too high to prevent accidental use of the wrong data.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied