Repository navigation
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-10-12 20:47 CST)
📝 Review Instructions
|
|
关于本文件中,1372行for循环中 for (index = 0; index < times; index++)
{
int delay_tick = 0;
rt_tick_t start_tick = 0;
rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp));
start_tick = rt_tick_get();
ret = netdev->ops->ping(netdev, (const char *)target_name, size, NETDEV_PING_RECV_TIMEO, &ping_resp, isbind);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("ping: from %s icmp_seq=%d timeout\n",
(ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr), index + 1);
}
else if (ret == -RT_ERROR)
{
rt_kprintf("ping: unknown %s %s\n",
(ip_addr_isany(&(ping_resp.ip_addr))) ? "host" : "address",
(ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr));
}
else
{
if (ping_resp.ttl == 0)
{
rt_kprintf("%d bytes from %s icmp_seq=%d time=%d ms\n",
ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ticks);
}
else
{
rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ttl, ping_resp.ticks);
}
received += 1;
if (ping_resp.ticks > max_time)
{
max_time = ping_resp.ticks;
}
else if (ping_resp.ticks < min_time)
{
min_time = ping_resp.ticks;
}
avg_time += ping_resp.ticks;
}
/* if the response time is more than NETDEV_PING_DELAY, no need to delay */
delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY;
rt_thread_delay(delay_tick);
}第1416行代码: delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY;其目的是最后一次ping就不需要再补齐时间delay。其中 index是严格小于times的为永假条件。且当delay_tick为0时会进入rt_thread_delay(0)函数,并被返回非法参数 for (index = 0; index < times; index++)
{
rt_tick_t start_tick = 0,cost_tick = 0,delay_tick = 0;
rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp));
start_tick = rt_tick_get();
ret = netdev->ops->ping(netdev, (const char *)target_name, size, NETDEV_PING_RECV_TIMEO, &ping_resp, isbind);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("ping: from %s icmp_seq=%d timeout\n",
(ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr), index + 1);
}
else if (ret == -RT_ERROR)
{
rt_kprintf("ping: unknown %s %s\n",
(ip_addr_isany(&(ping_resp.ip_addr))) ? "host" : "address",
(ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr));
}
else
{
if (ping_resp.ttl == 0)
{
rt_kprintf("%d bytes from %s icmp_seq=%d time=%d ms\n",
ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ticks);
}
else
{
rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ttl, ping_resp.ticks);
}
received += 1;
if (ping_resp.ticks > max_time)
{
max_time = ping_resp.ticks;
}
else if (ping_resp.ticks < min_time)
{
min_time = ping_resp.ticks;
}
avg_time += ping_resp.ticks;
}
/* if the response time is more than NETDEV_PING_DELAY, no need to delay */
cost_tick = rt_tick_get() - start_tick;
if (index != times - 1)
{
if (cost_tick < NETDEV_PING_DELAY)
{
delay_tick = NETDEV_PING_DELAY - cost_tick;
}
}
if(delay_tick > 0)
{
rt_thread_delay(delay_tick);
}
} |
|
在本pr中是否合适一并修改 |
…read_delay(0) The 'index == times' condition in the delay_tick calculation is never true, so the delay was still applied after the last ping, contrary to the comment. Skip the delay on the final ping by checking 'index != times - 1'. Also only call rt_thread_delay() when delay_tick > 0, since passing 0 returns -RT_EINVAL.
|
@CXSforHPU 非常感谢您的建议!已按您指出的两个问题一并修复并推送:
修复位于 |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
netdev_cmd_ping()computesavg_time = avg_time / receivedwhen printing ping statistics without checking thatreceivedis non-zero. When every ping attempt times out (target unreachable, the most common diagnostic scenario),receivedstays 0 and the integer division faults: SIGFPE on x86 and division-by-zero HardFault on Cortex-M, which kills the shell thread.Fixes #11852
你的解决方案是什么 (what is your solution)
Move the
avg_time / receivedcomputation inside the existingif (received > 0)guard so the division is never executed when no reply was received. Statistics still print100% packet losswithout min/max/avg values, matching the expected behavior described in #11852.Verified on
bsp/simulatorwith a test netdev whosepingop always returns-RT_ETIMEOUT:4 packets transmitted, 0 received, 100% packet loss, no crash请提供验证的bsp和config (provide the config and bsp)
BSP: bsp/simulator
.config: N/A (no configuration change)
action: local build with
scons -j4on bsp/simulator (Linux x86-64) passed; RT-Thread CI will run on this PR after creation.]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up