← index #18166Issue #18167
Related · high · value 5.265
QUERY · ISSUE

uctypes.bytearray_at() accepts negative address and segfaults on write

openby MaksimFengopened 2025-09-29updated 2026-03-20
bugproposed-close

Port, board and/or hardware

Unix port

MicroPython version

MicroPython v1.27.0-preview.107.gd1607598f on 2025-09-09; linux [GCC 14.2.0] version

Reproduction

try:
    import uctypes
except ImportError:
    print("SKIP missing uctypes")
else:
    try:
        ba = uctypes.bytearray_at(-9223372036854775808, 4)  # -2**63
        ba[0] = 1  # write to the mapped memory
        print('should not reach here')
    except Exception as e:
        print("EXC %s" % type(e).__name__)

Expected behaviour

A Python-level exception instead of process crash, e.g.:

  • ValueError/OverflowError for negative address; or

  • OSError if the address is not accessible.

At minimum, uctypes.bytearray_at() should reject negative addresses on 64-bit builds rather than silently converting them to huge unsigned pointers.

Observed behaviour

Program received signal SIGSEGV, Segmentation fault.
#0  mp_binary_set_val_array_from_int(typecode=0x1, p=0x8000000000000000, index=0x0, val=0x1)
#1  mp_binary_set_val_array(...)
#2  array_subscr(...)
#3  mp_obj_subscr(...)
#4  mp_execute_bytecode(...)

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

uctypes.bytes_at() accepts out-of-range (non-canonical) address and segfaults

openby MaksimFengopened 2025-09-29updated 2025-09-30
bug

Port, board and/or hardware

Unix port

MicroPython version

MicroPython v1.27.0-preview.107.gd1607598f on 2025-09-09; linux [GCC 14.2.0] version

Reproduction

A Python-level exception when the address is obviously invalid, e.g.:

  • ValueError/OverflowError for negative/out-of-range addresses, or
  • OSError if the runtime chooses to probe and detect unreadable memory on the unix port.

At minimum, reject negative addresses and detect addr + size overflow to avoid trivial VM crashes.

Expected behaviour


import uctypes
ptr = 1 << 48         
arr = uctypes.bytes_at(ptr, 8)
print('READ', arr)     

Observed behaviour

Program received signal SIGSEGV, Segmentation fault.
#0  qstr_compute_hash(data=0x1000000000000, len=0x8)
#1  mp_obj_new_str_copy(type=mp_type_bytes, data=0x1000000000000, len=0x8)
#2  mp_obj_new_bytes(...)
#3  uctypes_struct_bytes_at(ptr=0x1000000000000, size=0x8)
#4  fun_builtin_2_call(...)
#5  mp_call_function_n_kw(...)
#6  mp_execute_bytecode(...)
...


Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied