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.
pull/6612/head
Arrowana 2020-10-31 13:34:58 +11:00 zatwierdzone przez Damien George
rodzic 8a917ad252
commit 922f81dfd1
4 zmienionych plików z 26 dodań i 2 usunięć

Wyświetl plik

@ -40,7 +40,7 @@
#if !defined(MICROPY_MACHINE_MEM_GET_READ_ADDR) || !defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR)
STATIC uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) {
uintptr_t addr = mp_obj_int_get_truncated(addr_o);
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
if ((addr & (align - 1)) != 0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align);
}

Wyświetl plik

@ -47,7 +47,7 @@
#if MICROPY_PY_MACHINE
uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) {
uintptr_t addr = mp_obj_int_get_truncated(addr_o);
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
if ((addr & (align - 1)) != 0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align);
}

Wyświetl plik

@ -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")

Wyświetl plik

@ -2,3 +2,7 @@
ValueError
ValueError
TypeError
TypeError
TypeError
TypeError
TypeError