From c91ae4e83c5a1a82f647740277f7d4a462192160 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 22 Apr 2024 15:21:57 +1000 Subject: [PATCH] codal_port/microbit_microphone: Validate duration and rate args. Signed-off-by: Damien George --- src/codal_port/microbit_microphone.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/codal_port/microbit_microphone.c b/src/codal_port/microbit_microphone.c index d4be3d9..77b55f7 100644 --- a/src/codal_port/microbit_microphone.c +++ b/src/codal_port/microbit_microphone.c @@ -184,6 +184,14 @@ static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + // Validate arguments. + if (args[ARG_duration].u_int <= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("duration out of bounds")); + } + if (args[ARG_rate].u_int <= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds")); + } + // Create the AudioFrame to record into. size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000; microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int);