From 86c71a0307a509b6c47fbb7ce1ab2d827ff92653 Mon Sep 17 00:00:00 2001 From: Alessandro Gatti Date: Tue, 1 Oct 2024 21:40:06 +0200 Subject: [PATCH] esp32/machine_hw_spi: Reject invalid number of bits in constructor. This commit adds an extra bit of parameters validation to the SPI bus constructor on ESP32. Passing 0 as the number of bits would trigger a division by zero error when performing read/write operations on an SPI bus created in such a fashion. Fixes issue #5910. Signed-off-by: Alessandro Gatti --- ports/esp32/machine_hw_spi.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 4b3d392cb..6eb83fc09 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -196,6 +196,10 @@ static void machine_hw_spi_init_internal(machine_hw_spi_obj_t *self, mp_arg_val_ changed = true; } + if (args[ARG_bits].u_int != -1 && args[ARG_bits].u_int <= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid bits")); + } + if (args[ARG_bits].u_int != -1 && args[ARG_bits].u_int != self->bits) { self->bits = args[ARG_bits].u_int; changed = true;