drivers: uart_xmc4xxx: Don't recast to uint16_t* when returning value

Casting to uint16_t* can cause an unaligned usage fault when c is not
aligned to 2 bytes and can unintentionally overwrite data when c has a 1
byte memory size. Also there's no need to cast to uint16_t* because
returned words are 8-bit characaters as setup in the configuration.

Fixes the following usage fault error in tests/drivers/uart/uart_basic_api:
START - test_uart_poll_in
Please send characters to serial console
E: ***** USAGE FAULT *****
E:   Unaligned memory access
E: r0/a1:  0x00000000  r1/a2:  0x2000078f  r2/a3:  0x0c00453c
E: r3/a4:  0x00000000 r12/ip:  0x00000000 r14/lr:  0x0c003de5
E:  xpsr:  0x41000000
E: Faulting instruction address (r15/pc): 0x0c003de4
E: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
E: Current thread: 0x20000118 (unknown)
E: Halting system

Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
This commit is contained in:
Andriy Gelman 2022-05-15 14:34:59 -04:00 committed by Carles Cufí
parent d67a130368
commit 4c2ff6dc2f

View file

@ -23,7 +23,7 @@ static int uart_xmc4xxx_poll_in(const struct device *dev, unsigned char *c)
{
const struct uart_xmc4xx_config *config = dev->config;
*(uint16_t *)c = XMC_UART_CH_GetReceivedData(config->uart);
*c = (unsigned char)XMC_UART_CH_GetReceivedData(config->uart);
return 0;
}