From bd7abcda970e4e6ecd0f64621536870b49185594 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 25 Oct 2017 14:28:23 -0700 Subject: shared-bindings: Check that I2C and SPI reads and writes are given a buffer of at least 1. (#370) Fixes #358 --- shared-bindings/busio/I2C.c | 8 ++++++++ shared-bindings/busio/SPI.c | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'shared-bindings') diff --git a/shared-bindings/busio/I2C.c b/shared-bindings/busio/I2C.c index cdc960f80..dc1997034 100644 --- a/shared-bindings/busio/I2C.c +++ b/shared-bindings/busio/I2C.c @@ -201,6 +201,10 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, int32_t start = args[ARG_start].u_int; uint32_t length = bufinfo.len; normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); + if (length == 0) { + mp_raise_ValueError("Buffer must be at least length 1"); + } + uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, length); if (status != 0) { mp_raise_OSError(status); @@ -250,6 +254,10 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma uint32_t length = bufinfo.len; normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); + if (length == 0) { + mp_raise_ValueError("Buffer must be at least length 1"); + } + // do the transfer uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int, ((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool); diff --git a/shared-bindings/busio/SPI.c b/shared-bindings/busio/SPI.c index 12f888f23..dc136564d 100644 --- a/shared-bindings/busio/SPI.c +++ b/shared-bindings/busio/SPI.c @@ -231,6 +231,10 @@ STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_ uint32_t length = bufinfo.len; normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); + if (length == 0) { + mp_raise_ValueError("Buffer must be at least length 1"); + } + bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, length); if (!ok) { mp_raise_OSError(MP_EIO); @@ -269,6 +273,10 @@ STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_m uint32_t length = bufinfo.len; normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); + if (length == 0) { + mp_raise_ValueError("Buffer must be at least length 1"); + } + bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, length, args[ARG_write_value].u_int); if (!ok) { mp_raise_OSError(MP_EIO); -- cgit v1.2.3