From 186d1ca80ff6c7d9f9d2630190160e41a3bae593 Mon Sep 17 00:00:00 2001 From: lccosy <1191294205@qq.com> Date: Thu, 4 Jun 2026 23:01:09 +0800 Subject: [PATCH] arch/arm/gd32f4: fix NULL pointer dereference in arm_earlyserialinit. Add NULL check for g_uart_devs[i] before accessing ->priv in arm_earlyserialinit() loop. When a USART is not enabled in defconfig, g_uart_devs[i] is NULL, causing a HardFault crash during early boot. The bug occurs because the original code only checked g_uart_devs[i]->priv without first verifying g_uart_devs[i] is not NULL. On Cortex-M4, NULL pointer dereference reads from Flash vector table (0x00000000 maps to 0x08000000), returning a function pointer that causes BusFault when written to. This fix matches the existing NULL check pattern used in arm_serialinit() at line 2835 of the same file. Tested on mplant-gd32f450 board with only USART5 enabled in defconfig. Before fix: HardFault at boot (IPSR=3, PC=0x080003e0) After fix: System boots normally to NSH Shell Signed-off-by: lccosy <1191294205@qq.com> --- arch/arm/src/gd32f4/gd32f4xx_serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm/src/gd32f4/gd32f4xx_serial.c b/arch/arm/src/gd32f4/gd32f4xx_serial.c index f4a3659721d..7291a9dceec 100644 --- a/arch/arm/src/gd32f4/gd32f4xx_serial.c +++ b/arch/arm/src/gd32f4/gd32f4xx_serial.c @@ -2763,7 +2763,7 @@ void arm_earlyserialinit(void) for (i = 0; i < GD32_NUSART; i++) { - if (g_uart_devs[i]->priv) + if (g_uart_devs[i] && g_uart_devs[i]->priv) { up_disableusartint(g_uart_devs[i]->priv, 0); }