From 473611d51d6efb78e4a696b62334c5903d9ba286 Mon Sep 17 00:00:00 2001 From: Abdelatif Guettouche Date: Fri, 22 Apr 2022 02:47:58 +0200 Subject: [PATCH] system/ping: Guard against division by zero when calculating the RTT. Signed-off-by: Abdelatif Guettouche --- system/ping/ping.c | 15 +++++++++++---- system/ping6/ping6.c | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/system/ping/ping.c b/system/ping/ping.c index a01950a64..dd2d64350 100644 --- a/system/ping/ping.c +++ b/system/ping/ping.c @@ -242,10 +242,17 @@ static void ping_result(FAR const struct ping_result_s *result) result->extra / USEC_PER_MSEC); if (result->nreplies > 0) { - long avg = priv->tsum / result->nreplies; - long long tempnum = priv->tsum2 / result->nreplies - - (long long)avg * avg; - long tmdev = ub16toi(ub32sqrtub16(uitoub32(tempnum))); + long avg = 0; + long long tempnum = 0; + long tmdev = 0; + + if (priv->tsum > 0) + { + avg = priv->tsum / result->nreplies; + tempnum = priv->tsum2 / result->nreplies - + (long long)avg * avg; + tmdev = ub16toi(ub32sqrtub16(uitoub32(tempnum))); + } printf("rtt min/avg/max/mdev = %ld.%03ld/%ld.%03ld/" "%ld.%03ld/%ld.%03ld ms\n", diff --git a/system/ping6/ping6.c b/system/ping6/ping6.c index ac201d7c3..edd04eb42 100644 --- a/system/ping6/ping6.c +++ b/system/ping6/ping6.c @@ -239,10 +239,17 @@ static void ping6_result(FAR const struct ping6_result_s *result) result->extra / USEC_PER_MSEC); if (result->nreplies > 0) { - long avg = priv->tsum / result->nreplies; - long long tempnum = priv->tsum2 / result->nreplies - - (long long)avg * avg; - long tmdev = ub16toi(ub32sqrtub16(uitoub32(tempnum))); + long avg = 0; + long long tempnum = 0; + long tmdev = 0; + + if (priv->tsum > 0) + { + avg = priv->tsum / result->nreplies; + tempnum = priv->tsum2 / result->nreplies - + (long long)avg * avg; + tmdev = ub16toi(ub32sqrtub16(uitoub32(tempnum))); + } printf("rtt min/avg/max/mdev = %ld.%03ld/%ld.%03ld/" "%ld.%03ld/%ld.%03ld ms\n",