Switch to a shared piece of code to compute start and length of a

buffer from start, end and length. The old code miscomputed length
leading to writing and reading from memory past the end of the buffer.
Consolidating the code should make it easier to get right everywhere.
This commit is contained in:
Scott Shawcroft 2017-04-20 11:24:05 -07:00
parent 076ff82c46
commit 939c0045db
8 changed files with 111 additions and 72 deletions

View File

@ -207,6 +207,7 @@ SRC_C = \
lib/fatfs/ff.c \
lib/fatfs/option/ccsbcs.c \
lib/timeutils/timeutils.c \
lib/utils/buffer_helper.c \
lib/utils/context_manager_helpers.c \
lib/utils/interrupt_char.c \
lib/utils/pyexec.c \

View File

@ -171,6 +171,7 @@ LIB_SRC_C = $(addprefix lib/,\
mp-readline/readline.c \
netutils/netutils.c \
timeutils/timeutils.c \
utils/buffer_helper.c \
utils/context_manager_helpers.c \
utils/pyexec.c \
utils/pyhelp.c \

43
lib/utils/buffer_helper.c Normal file
View File

@ -0,0 +1,43 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "lib/utils/buffer_helper.h"
void normalize_buffer_bounds(int32_t* start, int32_t end, uint32_t* length) {
if (end < 0) {
end += *length;
} else if (((uint32_t) end) > *length) {
end = *length;
}
if (*start < 0) {
*start += *length;
}
if (end < *start) {
*length = 0;
} else {
*length = end - *start;
}
}

34
lib/utils/buffer_helper.h Normal file
View File

@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__
#define __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__
#include <stdint.h>
void normalize_buffer_bounds(int32_t* start, int32_t end, uint32_t* length);
#endif // __MICROPY_INCLUDED_LIB_UTILS_BUFFER_HELPER_H__

View File

@ -30,6 +30,7 @@
#include "shared-bindings/bitbangio/I2C.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/mperrno.h"
#include "py/runtime.h"
@ -172,21 +173,14 @@ STATIC mp_obj_t bitbangio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_a
check_lock(self);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
uint8_t status = shared_module_bitbangio_i2c_read(self,
args[ARG_address].u_int,
((uint8_t*)bufinfo.buf) + start,
len);
length);
if (status != 0) {
mp_raise_OSError(status);
}
@ -228,21 +222,13 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
// do the transfer
uint8_t status = shared_module_bitbangio_i2c_write(self, args[ARG_address].u_int,
((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool);
((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool);
if (status != 0) {
mp_raise_OSError(status);
}

View File

@ -185,6 +185,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock);
//|
//| Write the data contained in ``buf``. Requires the SPI being locked.
//|
// TODO(tannewt): Add support for start and end kwargs.
STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
mp_buffer_info_t src;
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
@ -203,6 +204,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write);
//|
//| Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked.
//|
// TODO(tannewt): Add support for start and end kwargs.
STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);

View File

@ -30,6 +30,7 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/I2C.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/runtime.h"
//| .. currentmodule:: busio
@ -187,18 +188,11 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args,
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len);
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
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);
}
@ -241,21 +235,14 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
// do the transfer
uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int,
((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool);
((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool);
if (status != 0) {
mp_raise_OSError(status);
}

View File

@ -32,6 +32,7 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/SPI.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/mperrno.h"
#include "py/nlr.h"
@ -217,19 +218,11 @@ STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len);
bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, length);
if (!ok) {
mp_raise_OSError(MP_EIO);
}
@ -262,19 +255,11 @@ STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_m
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
int32_t end = args[ARG_end].u_int;
if (end < 0) {
end += bufinfo.len;
}
uint32_t start = args[ARG_start].u_int;
uint32_t len = end - start;
if ((uint32_t) end < start) {
len = 0;
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
int32_t start = args[ARG_start].u_int;
uint32_t length = bufinfo.len;
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int);
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);
}