shared-bindings: Check that I2C and SPI reads and writes are given a buffer of at least 1. (#370)

Fixes #358
This commit is contained in:
Scott Shawcroft 2017-10-25 14:28:23 -07:00 committed by Dan Halbert
parent e08241de41
commit bd7abcda97
2 changed files with 16 additions and 0 deletions

View File

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

View File

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