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.
This commit is contained in:
Jeff Epler 2020-01-08 09:43:13 -06:00
parent 5baaac55ce
commit 6735283d8f

View File

@ -33,6 +33,7 @@
#include "lib/oofatfs/diskio.h" #include "lib/oofatfs/diskio.h"
#include "py/mpstate.h" #include "py/mpstate.h"
#include "py/obj.h" #include "py/obj.h"
#include "py/objstr.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/os/__init__.h" #include "shared-bindings/os/__init__.h"
@ -195,11 +196,11 @@ MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
//| //|
STATIC mp_obj_t os_urandom(mp_obj_t size_in) { STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
mp_int_t size = mp_obj_get_int(size_in); mp_int_t size = mp_obj_get_int(size_in);
uint8_t tmp[size]; mp_obj_str_t *result = MP_OBJ_TO_PTR(mp_obj_new_bytes_of_zeros(size));
if (!common_hal_os_urandom(tmp, size)) { if (!common_hal_os_urandom((uint8_t*) result->data, size)) {
mp_raise_NotImplementedError(translate("No hardware random available")); mp_raise_NotImplementedError(translate("No hardware random available"));
} }
return mp_obj_new_bytes(tmp, size); return result;
} }
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);