AP_BattMonitor: add assertion to avoid memory over-read

This commit is contained in:
Peter Barker 2021-06-09 13:26:55 +10:00 committed by Andrew Tridgell
parent 170062804f
commit 023c84f84a
1 changed files with 6 additions and 0 deletions

View File

@ -81,6 +81,12 @@ void AP_BattMonitor_SMBus_Generic::timer()
}
}
// we loop over something limted by
// BATTMONITOR_SMBUS_NUM_CELLS_MAX but assign into something
// limited by AP_BATT_MONITOR_CELLS_MAX - so make sure we won't
// over-write:
static_assert(BATTMONITOR_SMBUS_NUM_CELLS_MAX <= ARRAY_SIZE(_state.cell_voltages.cells), "BATTMONITOR_SMBUS_NUM_CELLS_MAX must be <= number of cells in state voltages");
// read cell voltages
for (uint8_t i = 0; i < (_cell_count_fixed ? _cell_count : BATTMONITOR_SMBUS_NUM_CELLS_MAX); i++) {
if (read_word(smbus_cell_ids[i], data) && (data > 0) && (data < UINT16_MAX)) {