Skip to content

Commit f4a12dc

Browse files
committed
py/objarray: Disallow slice-assignment to read-only memoryview.
Also comes with a test for this. Fixes issue adafruit#2904.
1 parent 23ccb3e commit f4a12dc

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

py/objarray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
418418
uint8_t* dest_items = o->items;
419419
#if MICROPY_PY_BUILTINS_MEMORYVIEW
420420
if (o->base.type == &mp_type_memoryview) {
421+
if ((o->typecode & 0x80) == 0) {
422+
// store to read-only memoryview not allowed
423+
return MP_OBJ_NULL;
424+
}
421425
if (len_adj != 0) {
422426
goto compat_error;
423427
}

tests/basics/memoryview1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
m[0] = 1
1919
except TypeError:
2020
print("TypeError")
21+
try:
22+
m[0:2] = b'00'
23+
except TypeError:
24+
print("TypeError")
2125

2226
# test writing to bytearray
2327
b = bytearray(b)

0 commit comments

Comments
 (0)