Skip to content

Commit 6735283

Browse files
committed
os: Don't require an on-stack buffer
This allows urandom requests of even 100k bytes to succeed on a fresh VM session on a Metro M4 express.
1 parent 5baaac5 commit 6735283

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

shared-bindings/os/__init__.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "lib/oofatfs/diskio.h"
3434
#include "py/mpstate.h"
3535
#include "py/obj.h"
36+
#include "py/objstr.h"
3637
#include "py/runtime.h"
3738
#include "shared-bindings/os/__init__.h"
3839

@@ -195,11 +196,11 @@ MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
195196
//|
196197
STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
197198
mp_int_t size = mp_obj_get_int(size_in);
198-
uint8_t tmp[size];
199-
if (!common_hal_os_urandom(tmp, size)) {
199+
mp_obj_str_t *result = MP_OBJ_TO_PTR(mp_obj_new_bytes_of_zeros(size));
200+
if (!common_hal_os_urandom((uint8_t*) result->data, size)) {
200201
mp_raise_NotImplementedError(translate("No hardware random available"));
201202
}
202-
return mp_obj_new_bytes(tmp, size);
203+
return result;
203204
}
204205
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
205206

0 commit comments

Comments
 (0)