From eee96b72b4d183ce3ac63ca0405da74578e795df Mon Sep 17 00:00:00 2001 From: Zhaoqi Xu Date: Tue, 6 Oct 2026 14:07:59 +0800 Subject: [PATCH] [components][netdev] fix divide-by-zero in ping statistics when no reply netdev_cmd_ping() computed avg_time / received before checking that any reply arrived. When every request times out, received is 0 and the integer division traps (SIGFPE on the simulator, a fault on Cortex-M), taking the shell thread down. Only compute the average inside the existing received > 0 branch where it is printed. Fixes #11852 --- components/net/netdev/src/netdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index 5a4056b97a7b..b57e844504db 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -1425,12 +1425,12 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_ /* print ping statistics */ loss = (uint32_t)((1 - ((float)received) / index) * 100); - avg_time = (uint32_t)(avg_time / received); rt_kprintf("\n--- %s ping statistics ---\n", inet_ntoa(ping_resp.ip_addr)); rt_kprintf("%d packets transmitted, %d received, %d%% packet loss\n", index, received, loss); if (received > 0) { + avg_time = (uint32_t)(avg_time / received); rt_kprintf("minimum = %dms, maximum = %dms, average = %dms\n", min_time, max_time, avg_time); }