From f4a12dca58c53f11a6e89424a47158fe6e48ade9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 27 Feb 2017 16:09:57 +1100 Subject: [PATCH] py/objarray: Disallow slice-assignment to read-only memoryview. Also comes with a test for this. Fixes issue #2904. --- py/objarray.c | 4 ++++ tests/basics/memoryview1.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/py/objarray.c b/py/objarray.c index 1b590f3c05..a84a631519 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -418,6 +418,10 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value uint8_t* dest_items = o->items; #if MICROPY_PY_BUILTINS_MEMORYVIEW if (o->base.type == &mp_type_memoryview) { + if ((o->typecode & 0x80) == 0) { + // store to read-only memoryview not allowed + return MP_OBJ_NULL; + } if (len_adj != 0) { goto compat_error; } diff --git a/tests/basics/memoryview1.py b/tests/basics/memoryview1.py index 019a1179f8..a771acdda9 100644 --- a/tests/basics/memoryview1.py +++ b/tests/basics/memoryview1.py @@ -18,6 +18,10 @@ try: m[0] = 1 except TypeError: print("TypeError") +try: + m[0:2] = b'00' +except TypeError: + print("TypeError") # test writing to bytearray b = bytearray(b)