From 023c84f84a564ee61d850bf15e076335ace8b714 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Wed, 9 Jun 2021 13:26:55 +1000 Subject: [PATCH] AP_BattMonitor: add assertion to avoid memory over-read --- libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp index 169dab3a35..337cd9f9ee 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Generic.cpp @@ -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)) {