From 3c82ac6043539dd69a0c2d9fea7c970591d83b04 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 23 Nov 2023 19:56:54 -0600 Subject: [PATCH] AP_HAL_SITL: introduce get_HAL_mutable() to complement get_HAL() Returns a mutable reference to the same HAL for certain purposes where the HAL needs to be mutated to avoid UB problems with casting away const and to make the fact that mutation is happening obvious. --- libraries/AP_HAL_SITL/HAL_SITL_Class.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libraries/AP_HAL_SITL/HAL_SITL_Class.cpp b/libraries/AP_HAL_SITL/HAL_SITL_Class.cpp index 8a7537ab97..bf8bc07351 100644 --- a/libraries/AP_HAL_SITL/HAL_SITL_Class.cpp +++ b/libraries/AP_HAL_SITL/HAL_SITL_Class.cpp @@ -33,7 +33,7 @@ using namespace HALSITL; -HAL_SITL& hal_sitl = (HAL_SITL&)AP_HAL::get_HAL(); +HAL_SITL& hal_sitl = (HAL_SITL&)AP_HAL::get_HAL_mutable(); static Storage sitlStorage; static SITL_State sitlState; @@ -299,9 +299,14 @@ void HAL_SITL::actually_reboot() AP_HAL::panic("PANIC: REBOOT FAILED: %s", strerror(errno)); } +static HAL_SITL hal_sitl_inst; + const AP_HAL::HAL& AP_HAL::get_HAL() { - static const HAL_SITL hal; - return hal; + return hal_sitl_inst; +} + +AP_HAL::HAL& AP_HAL::get_HAL_mutable() { + return hal_sitl_inst; } #endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL