From 922f81dfd1b94e23f23f05aac0ecafac8645ce46 Mon Sep 17 00:00:00 2001 From: Arrowana Date: Sat, 31 Oct 2020 13:34:58 +1100 Subject: extmod/machine_mem: Only allow integers in machine.memX subscript. Prior to this change machine.mem32['foo'] (or using any other non-integer subscript) could result in a fault due to 'foo' being interpreted as an integer. And when writing code it's hard to tell if the fault is due to a bad subscript type, or an integer subscript that specifies an invalid memory address. The type of the object used in the subscript is now tested to be an integer by using mp_obj_get_int_truncated instead of mp_obj_int_get_truncated. The performance hit of this change is minimal, and machine.memX objects are more for convenience than performance (there are many other ways to read/write memory in a faster way), Fixes issue #6588. --- tests/extmod/machine1.py | 20 ++++++++++++++++++++ tests/extmod/machine1.py.exp | 4 ++++ 2 files changed, 24 insertions(+) (limited to 'tests/extmod') diff --git a/tests/extmod/machine1.py b/tests/extmod/machine1.py index 6ff38cc05..0c7f8122f 100644 --- a/tests/extmod/machine1.py +++ b/tests/extmod/machine1.py @@ -26,3 +26,23 @@ try: del machine.mem8[0] except TypeError: print("TypeError") + +try: + machine.mem8[0:1] +except TypeError: + print("TypeError") + +try: + machine.mem8[0:1] = 10 +except TypeError: + print("TypeError") + +try: + machine.mem8["hello"] +except TypeError: + print("TypeError") + +try: + machine.mem8["hello"] = 10 +except TypeError: + print("TypeError") diff --git a/tests/extmod/machine1.py.exp b/tests/extmod/machine1.py.exp index bb421ea5c..250485969 100644 --- a/tests/extmod/machine1.py.exp +++ b/tests/extmod/machine1.py.exp @@ -2,3 +2,7 @@ ValueError ValueError TypeError +TypeError +TypeError +TypeError +TypeError -- cgit v1.2.3