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)) {