AP_HAL: use array of UART drivers instead of consecutive variables

Avoids UB-inducing assumption that UART drivers are consecutive in the
serial() function.
This commit is contained in:
Thomas Watson 2023-11-15 11:56:29 -06:00 committed by Andrew Tridgell
parent d90224280e
commit de45437704
2 changed files with 17 additions and 26 deletions

View File

@ -15,11 +15,10 @@ 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");
if (sernum >= num_serial) {
static_assert(sizeof(mapping) == ARRAY_SIZE(uart_array), "array size must match mapping");
if (sernum >= ARRAY_SIZE(uart_array)) {
return nullptr;
}
return uart_array[mapping[sernum]];

View File

@ -53,16 +53,17 @@ public:
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),
@ -112,19 +113,6 @@ public:
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;
@ -151,6 +139,10 @@ public:
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