From 6b8bcb6a2964ef9b8ddc0fa24fa6455aeeec5317 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 22 Feb 2026 09:16:05 +0100 Subject: [PATCH] extmod/modos: Raise an error at a negative argument of os.urandom(). Fixes issue #18825. Signed-off-by: robert-hh --- extmod/modos.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extmod/modos.c b/extmod/modos.c index b1dd267f88..c330162e10 100644 --- a/extmod/modos.c +++ b/extmod/modos.c @@ -149,6 +149,9 @@ static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_dupterm_notify_obj, mp_os_dupterm_notify) // This wraps the port-specific mp_hal_get_random(), which is usually defined in mphalport.c. static mp_obj_t mp_os_urandom(mp_obj_t num) { mp_int_t n = mp_obj_get_int(num); + if (n < 0) { + mp_raise_ValueError(NULL); + } vstr_t vstr; vstr_init_len(&vstr, n); mp_hal_get_random(n, (uint8_t *)vstr.buf);