summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2017-10-25 14:28:23 -0700
committerDan Halbert <halbert@halwitz.org>2017-10-25 17:28:23 -0400
commitbd7abcda970e4e6ecd0f64621536870b49185594 (patch)
tree7c2cbaeff76e0be1909cecc06ac0408cacdeb154
parente08241de41e07ae5d7b57492a81746002c65c329 (diff)
shared-bindings: Check that I2C and SPI reads and writes are given a buffer of at least 1. (#370)
Fixes #358
-rw-r--r--shared-bindings/busio/I2C.c8
-rw-r--r--shared-bindings/busio/SPI.c8
2 files changed, 16 insertions, 0 deletions
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);