This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 473611d51 system/ping: Guard against division by zero when calculating 
the RTT.
473611d51 is described below

commit 473611d51d6efb78e4a696b62334c5903d9ba286
Author: Abdelatif Guettouche <[email protected]>
AuthorDate: Fri Apr 22 02:47:58 2022 +0200

    system/ping: Guard against division by zero when calculating the RTT.
    
    Signed-off-by: Abdelatif Guettouche <[email protected]>
---
 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",

Reply via email to