Skip to content

Commit

Permalink
AP_HAL: use array of UART drivers instead of consecutive variables
Browse files Browse the repository at this point in the history
Avoids UB-inducing assumption that UART drivers are consecutive in the
serial() function.
  • Loading branch information
tpwrules committed Nov 15, 2023
1 parent 784f435 commit 8e4070d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
1 change: 0 additions & 1 deletion libraries/AP_HAL/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ HAL::FunCallbacks::FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void))
// access serial ports using SERIALn numbering
AP_HAL::UARTDriver* AP_HAL::HAL::serial(uint8_t sernum) const
{
UARTDriver **uart_array = const_cast<UARTDriver**>(&uartA);
// this mapping captures the historical use of uartB as SERIAL3
const uint8_t mapping[] = { 0, 2, 3, 1, 4, 5, 6, 7, 8, 9 };
static_assert(sizeof(mapping) == num_serial, "num_serial must match mapping");
Expand Down
38 changes: 15 additions & 23 deletions libraries/AP_HAL/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ class AP_HAL::HAL {
AP_HAL::CANIface** _can_ifaces)
#endif
:
uartA(_uartA),
uartB(_uartB),
uartC(_uartC),
uartD(_uartD),
uartE(_uartE),
uartF(_uartF),
uartG(_uartG),
uartH(_uartH),
uartI(_uartI),
uartJ(_uartJ),
uart_array{
_uartA,
_uartB,
_uartC,
_uartD,
_uartE,
_uartF,
_uartG,
_uartH,
_uartI,
_uartJ},
i2c_mgr(_i2c_mgr),
spi(_spi),
wspi(_wspi),
Expand Down Expand Up @@ -112,19 +113,6 @@ class AP_HAL::HAL {

virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0;

private:
// the uartX ports must be contiguous in ram for the serial() method to work
AP_HAL::UARTDriver* uartA;
AP_HAL::UARTDriver* uartB UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartC UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartD UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartE UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartF UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartG UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartH UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartI UNUSED_PRIVATE_MEMBER;
AP_HAL::UARTDriver* uartJ UNUSED_PRIVATE_MEMBER;

public:
AP_HAL::I2CDeviceManager* i2c_mgr;
AP_HAL::SPIDeviceManager* spi;
Expand All @@ -151,6 +139,10 @@ class AP_HAL::HAL {

static constexpr uint8_t num_serial = 10;

private:
AP_HAL::UARTDriver* uart_array[num_serial];

public:
#if AP_SIM_ENABLED && CONFIG_HAL_BOARD != HAL_BOARD_SITL
AP_HAL::SIMState *simstate;
#endif
Expand Down

0 comments on commit 8e4070d

Please sign in to comment.