Skip to content

os.urandom(-1) crash #18825

Description

@GGAutomaton

Port, board and/or hardware

unix

MicroPython version

MicroPython v1.28.0-preview.174.ga8b71559cf on 2026-02-16; linux [GCC 15.2.0] version

Reproduction

import os
os.urandom(-1)

Expected behaviour

ValueError: negative argument not allowed

Observed behaviour

AddressSanitizer:DEADLYSIGNAL
=================================================================
==29603==ERROR: AddressSanitizer: SEGV on unknown address 0x7ffa15c00000 (pc 0x7ffa165194ab bp 0x7ffc170fef90 sp 0x7ffc170fef30 T0)
==29603==The signal is caused by a WRITE memory access.
    #0 0x7ffa165194ab  (linux-vdso.so.1+0x14ab) (BuildId: 9b63024b6069edc1f3baef9d1ce5d39a51e32768)
    #1 0x7ffa1564df0e in getrandom_vdso ../sysdeps/unix/sysv/linux/getrandom.c:211

==29603==Register values:
rax = 0x0000000000000000  rbx = 0x000000007ffff000  rcx = 0x0000000001ffa00f  rdx = 0x00007ffc170fef60  
rdi = 0x00007ffa15bfffe0  rsi = 0x00007ffa1637cf60  rbp = 0x00007ffc170fef90  rsp = 0x00007ffc170fef30  
 r8 = 0x000000007ffff000   r9 = 0xffffffffffffffff  r10 = 0x00007ffa1637cf00  r11 = 0x0000000000000246  
r12 = 0x00007ffa15a813a0  r13 = 0xffffffffffffffff  r14 = 0x00007ffa1637cf00  r15 = 0x0000000001ffffc0  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (linux-vdso.so.1+0x14ab) (BuildId: 9b63024b6069edc1f3baef9d1ce5d39a51e32768) 
==29603==ABORTING

Additional Information

No, I've provided everything above.

Credit: Yuan (@Reset816) and me.

Code of Conduct

Yes, I agree

Activity

  1. robert-hh commented on Feb 17, 2026

    @robert-hh
    Contributor

    os.urandom(-1) bucks at the other ports as well. Tried with MIMXRT, SAMD, ESP32, ESP8266, RP2, NRF, STM32 and W60X. The only tested port which does not stall or reboot is the Renesas port, because it does not support os.urandom().
    If needed, I can make a PR to fix it at these ports, by either treating negative args a 0, returning an empty vector, or raising an error (CPython compatible).

    Edit: The Renesas port supports os.urandom(), but just for the Portenta C33 board.

  2. Josverl commented on Feb 17, 2026

    @Josverl
    SponsorContributor

    Raising an error would be the best IMHO

  3. projectgus commented on Feb 17, 2026

    @projectgus
    Contributor

    Agree re: error, thanks @robert-hh for picking this up!

  4. added this to the release-1.28.0 milestone on Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugextmodRelates to extmod/ directory in sourceport-unix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions