From 13ec6f9a24ac3398c3a7b59818f2b3cc8297c23e Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 17 Oct 2025 10:19:51 +1100 Subject: [PATCH] AP_Baro: adjust handling of instance/driver arrays to use ARRAY_SIZE and iterator if you are about to index something, directly use its size for your sanity check. Or avoid the check altogether by using an iterator. --- libraries/AP_Baro/AP_Baro.cpp | 12 ++++---- libraries/AP_Baro/AP_Baro.h | 2 +- libraries/AP_Baro/AP_Baro_DroneCAN.cpp | 42 +++++++++++++------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/libraries/AP_Baro/AP_Baro.cpp b/libraries/AP_Baro/AP_Baro.cpp index 10ee0c6552d18..b67981d8081d2 100644 --- a/libraries/AP_Baro/AP_Baro.cpp +++ b/libraries/AP_Baro/AP_Baro.cpp @@ -492,7 +492,7 @@ bool AP_Baro::_add_backend(AP_Baro_Backend *backend) if (!backend) { return false; } - if (_num_drivers >= BARO_MAX_DRIVERS) { + if (_num_drivers >= ARRAY_SIZE(drivers)) { AP_HAL::panic("Too many barometer drivers"); } drivers[_num_drivers++] = backend; @@ -520,8 +520,8 @@ bool AP_Baro::_have_i2c_driver(uint8_t bus, uint8_t address) const */ #define ADD_BACKEND(backend) \ do { _add_backend(backend); \ - if (_num_drivers == BARO_MAX_DRIVERS || \ - _num_sensors == BARO_MAX_INSTANCES) { \ + if (_num_drivers == ARRAY_SIZE(drivers) || \ + _num_sensors == ARRAY_SIZE(sensors)) { \ return; \ } \ } while (0) @@ -542,8 +542,8 @@ void AP_Baro::init(void) } // zero bus IDs before probing - for (uint8_t i = 0; i < BARO_MAX_INSTANCES; i++) { - sensors[i].bus_id.set(0); + for (auto &sensor : sensors) { + sensor.bus_id.set(0); } #if AP_SIM_BARO_ENABLED @@ -975,7 +975,7 @@ float AP_Baro::thrust_pressure_correction(uint8_t instance) */ uint8_t AP_Baro::register_sensor(void) { - if (_num_sensors >= BARO_MAX_INSTANCES) { + if (_num_sensors >= ARRAY_SIZE(sensors)) { AP_HAL::panic("Too many barometers"); } return _num_sensors++; diff --git a/libraries/AP_Baro/AP_Baro.h b/libraries/AP_Baro/AP_Baro.h index 232d7f18d4c7d..520e10094594c 100644 --- a/libraries/AP_Baro/AP_Baro.h +++ b/libraries/AP_Baro/AP_Baro.h @@ -285,7 +285,7 @@ class AP_Baro }; #endif - struct sensor { + struct { uint32_t last_update_ms; // last update time in ms uint32_t last_change_ms; // last update time in ms that included a change in reading from previous readings float pressure; // pressure in Pascal diff --git a/libraries/AP_Baro/AP_Baro_DroneCAN.cpp b/libraries/AP_Baro/AP_Baro_DroneCAN.cpp index 07975e1082323..4703b473097f1 100644 --- a/libraries/AP_Baro/AP_Baro_DroneCAN.cpp +++ b/libraries/AP_Baro/AP_Baro_DroneCAN.cpp @@ -35,32 +35,32 @@ AP_Baro_Backend* AP_Baro_DroneCAN::probe(AP_Baro &baro) WITH_SEMAPHORE(_sem_registry); AP_Baro_DroneCAN* backend = nullptr; - for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) { - if (_detected_modules[i].driver == nullptr && _detected_modules[i].ap_dronecan != nullptr) { + for (auto &detected_module : _detected_modules) { + if (detected_module.driver == nullptr && detected_module.ap_dronecan != nullptr) { backend = NEW_NOTHROW AP_Baro_DroneCAN(baro); if (backend == nullptr) { AP::can().log_text(AP_CANManager::LOG_ERROR, LOG_TAG, "Failed register DroneCAN Baro Node %d on Bus %d\n", - _detected_modules[i].node_id, - _detected_modules[i].ap_dronecan->get_driver_index()); + detected_module.node_id, + detected_module.ap_dronecan->get_driver_index()); } else { - _detected_modules[i].driver = backend; + detected_module.driver = backend; backend->_pressure = 0; backend->_pressure_count = 0; - backend->_ap_dronecan = _detected_modules[i].ap_dronecan; - backend->_node_id = _detected_modules[i].node_id; + backend->_ap_dronecan = detected_module.ap_dronecan; + backend->_node_id = detected_module.node_id; backend->_instance = backend->_frontend.register_sensor(); backend->set_bus_id(backend->_instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN, - _detected_modules[i].ap_dronecan->get_driver_index(), + detected_module.ap_dronecan->get_driver_index(), backend->_node_id, 0)); AP::can().log_text(AP_CANManager::LOG_INFO, LOG_TAG, "Registered DroneCAN Baro Node %d on Bus %d\n", - _detected_modules[i].node_id, - _detected_modules[i].ap_dronecan->get_driver_index()); + detected_module.node_id, + detected_module.ap_dronecan->get_driver_index()); } break; } @@ -73,29 +73,29 @@ AP_Baro_DroneCAN* AP_Baro_DroneCAN::get_dronecan_backend(AP_DroneCAN* ap_droneca if (ap_dronecan == nullptr) { return nullptr; } - for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) { - if (_detected_modules[i].driver != nullptr && - _detected_modules[i].ap_dronecan == ap_dronecan && - _detected_modules[i].node_id == node_id) { - return _detected_modules[i].driver; + for (auto &detected_module : _detected_modules) { + if (detected_module.driver != nullptr && + detected_module.ap_dronecan == ap_dronecan && + detected_module.node_id == node_id) { + return detected_module.driver; } } if (create_new) { bool already_detected = false; //Check if there's an empty spot for possible registration - for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) { - if (_detected_modules[i].ap_dronecan == ap_dronecan && _detected_modules[i].node_id == node_id) { + for (auto &detected_module : _detected_modules) { + if (detected_module.ap_dronecan == ap_dronecan && detected_module.node_id == node_id) { //Already Detected already_detected = true; break; } } if (!already_detected) { - for (uint8_t i = 0; i < BARO_MAX_DRIVERS; i++) { - if (_detected_modules[i].ap_dronecan == nullptr) { - _detected_modules[i].ap_dronecan = ap_dronecan; - _detected_modules[i].node_id = node_id; + for (auto &detected_module : _detected_modules) { + if (detected_module.ap_dronecan == nullptr) { + detected_module.ap_dronecan = ap_dronecan; + detected_module.node_id = node_id; break; } }