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 <a.gatti@frob.it>
This commit is contained in:
Alessandro Gatti
2024-10-01 21:40:06 +02:00
committed by Damien George
parent 9ea8d2a031
commit 86c71a0307

View File

@@ -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;