Skip to content

Commit bd7abcd

Browse files
tannewtdhalbert
authored andcommitted
shared-bindings: Check that I2C and SPI reads and writes are given a buffer of at least 1. (adafruit#370)
Fixes adafruit#358
1 parent e08241d commit bd7abcd

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

shared-bindings/busio/I2C.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args,
201201
int32_t start = args[ARG_start].u_int;
202202
uint32_t length = bufinfo.len;
203203
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
204+
if (length == 0) {
205+
mp_raise_ValueError("Buffer must be at least length 1");
206+
}
207+
204208
uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, length);
205209
if (status != 0) {
206210
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
250254
uint32_t length = bufinfo.len;
251255
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
252256

257+
if (length == 0) {
258+
mp_raise_ValueError("Buffer must be at least length 1");
259+
}
260+
253261
// do the transfer
254262
uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int,
255263
((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool);

shared-bindings/busio/SPI.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_
231231
uint32_t length = bufinfo.len;
232232
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
233233

234+
if (length == 0) {
235+
mp_raise_ValueError("Buffer must be at least length 1");
236+
}
237+
234238
bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, length);
235239
if (!ok) {
236240
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
269273
uint32_t length = bufinfo.len;
270274
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
271275

276+
if (length == 0) {
277+
mp_raise_ValueError("Buffer must be at least length 1");
278+
}
279+
272280
bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, length, args[ARG_write_value].u_int);
273281
if (!ok) {
274282
mp_raise_OSError(MP_EIO);

0 commit comments

Comments
 (0)