From fc560e82199c6bda2d7d320094d10ef03a2bbdea Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Wed, 5 Jun 2024 00:58:27 +1000 Subject: [PATCH] GCS_MAVLink: avoid casting DroneCAN backend to incorrect type - split get_type into allocated_type and configured_type - check allocated type rather than configured type when looking at backends Prevents overwrite of random memory when backends are changed at runtime. --- libraries/GCS_MAVLink/GCS_Common.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/GCS_MAVLink/GCS_Common.cpp b/libraries/GCS_MAVLink/GCS_Common.cpp index bf273c42e0..4189392e76 100644 --- a/libraries/GCS_MAVLink/GCS_Common.cpp +++ b/libraries/GCS_MAVLink/GCS_Common.cpp @@ -377,7 +377,9 @@ bool GCS_MAVLINK::send_battery_status() for(uint8_t i = 0; i < AP_BATT_MONITOR_MAX_INSTANCES; i++) { const uint8_t battery_id = (last_battery_status_idx + 1) % AP_BATT_MONITOR_MAX_INSTANCES; - if (battery.get_type(battery_id) != AP_BattMonitor::Type::NONE) { + const auto configured_type = battery.configured_type(battery_id); + if (configured_type != AP_BattMonitor::Type::NONE && + configured_type == battery.allocated_type(battery_id)) { CHECK_PAYLOAD_SIZE(BATTERY_STATUS); send_battery_status(battery_id); last_battery_status_idx = battery_id;