[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
This commit is contained in:
Zhaoqi Xu authored and Rbb666 committed 2026-10-14 09:33:19 +08:00
1 parent 517eecbe4a
commit 8ec9d87959
1 file changed
+1 -1
+1 -1
View File
@@ -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);
}