micropython/tests/basics/slice_indices.py
Damien George f397a3ec31 py/objslice: Validate that the argument to indices() is an integer.
Otherwise passing in a non-integer can lead to an invalid memory access.

Thanks to Junwha Hong and Wonil Jang @S2Lab, UNIST for finding the issue.

Fixes issue #13007.

Signed-off-by: Damien George <damien@micropython.org>
2023-11-21 22:28:57 +11:00

33 lines
691 B
Python

# Test builtin slice indices resolution
# A class that returns an item key
class A:
def __getitem__(self, idx):
return idx
# Make sure that we have slices and .indices()
try:
A()[2:5].indices(10)
except:
print("SKIP")
raise SystemExit
print(A()[:].indices(10))
print(A()[2:].indices(10))
print(A()[:7].indices(10))
print(A()[2:7].indices(10))
print(A()[2:7:2].indices(10))
print(A()[2:7:-2].indices(10))
print(A()[7:2:2].indices(10))
print(A()[7:2:-2].indices(10))
print(A()[2:7:2].indices(5))
print(A()[2:7:-2].indices(5))
print(A()[7:2:2].indices(5))
print(A()[7:2:-2].indices(5))
try:
print(A()[::].indices(None))
except TypeError:
print("TypeError")