From f854477efed20e3ba7d7ef1348c4884af74dc882 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Tue, 2 Feb 2021 20:32:45 -0700 Subject: [PATCH] AP_BattMonitor: Limit the scope of possible corruption if the NeoDesigns gets a bad cell count --- libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.cpp | 2 +- libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.cpp index d3877f72bd..ae3be521d6 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.cpp @@ -24,7 +24,7 @@ void AP_BattMonitor_SMBus_NeoDesign::timer() // Get the cell count once, it's not likely to change in flight if (_cell_count == 0) { if (read_word(BATTMONITOR_ND_CELL_COUNT, data)) { - _cell_count = data; + _cell_count = MIN(data, max_cell_count); // never read in more cells then we can store } else { return; // something wrong, don't try anything else } diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.h b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.h index 0c8b56cb7b..b27b37180c 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_NeoDesign.h @@ -15,4 +15,6 @@ private: void timer(void) override; uint8_t _cell_count; + + static const constexpr uint8_t max_cell_count = 10; };