From 8edfdb02ed1fd73568ef9e9522634e65791dbd69 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Sun, 1 Sep 2024 14:45:36 +0200 Subject: [PATCH] rtos: chibios: fix version display The field 'struct chibios_chdebug::ch_version' is 16 bits wide, so using le_to_h_u32() and be_to_h_u32() overflows in the following fields of the struct. Restrict the endianness conversion to 16 bits and use the target endianness dependent target_buffer_get_u16(). Convert the 'struct chibios_chdebug::ch_version' to an array of uint8_t. Change-Id: Iaa80e9cb1a65c27512919398b8ffbf14e5c240cd Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/8473 Tested-by: jenkins --- src/rtos/chibios.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/rtos/chibios.c b/src/rtos/chibios.c index c1e4e8419..f4ee33a49 100644 --- a/src/rtos/chibios.c +++ b/src/rtos/chibios.c @@ -31,7 +31,7 @@ struct chibios_chdebug { char ch_identifier[4]; /**< @brief Always set to "main". */ uint8_t ch_zero; /**< @brief Must be zero. */ uint8_t ch_size; /**< @brief Size of this structure. */ - uint16_t ch_version; /**< @brief Encoded ChibiOS/RT version. */ + uint8_t ch_version[2]; /**< @brief Encoded ChibiOS/RT version. */ uint8_t ch_ptrsize; /**< @brief Size of a pointer. */ uint8_t ch_timesize; /**< @brief Size of a @p systime_t. */ uint8_t ch_threadsize; /**< @brief Size of a @p Thread struct. */ @@ -171,13 +171,7 @@ static int chibios_update_memory_signature(struct rtos *rtos) " expected. Assuming compatibility..."); } - /* Convert endianness of version field */ - const uint8_t *versiontarget = (const uint8_t *) - &signature->ch_version; - signature->ch_version = rtos->target->endianness == TARGET_LITTLE_ENDIAN ? - le_to_h_u32(versiontarget) : be_to_h_u32(versiontarget); - - const uint16_t ch_version = signature->ch_version; + const uint16_t ch_version = target_buffer_get_u16(rtos->target, signature->ch_version); LOG_INFO("Successfully loaded memory map of ChibiOS/RT target " "running version %i.%i.%i", GET_CH_KERNEL_MAJOR(ch_version), GET_CH_KERNEL_MINOR(ch_version), GET_CH_KERNEL_PATCH(ch_version));