Sometimes floating point imprecision in the calculation of the standard
deviation of round-trip latency by ping(8) / ping6(8) leads to attempting
to evaluate the sqrt(3) of a negative value:

$ ping -c 1 www.google.com
PING www.google.com (150.101.170.166): 56 data bytes
64 bytes from 150.101.170.166: icmp_seq=0 ttl=61 time=20.623 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/std-dev = 20.623/20.623/20.623/-nan ms

Index: ping.c
===================================================================
RCS file: /cvs/src/sbin/ping/ping.c,v
retrieving revision 1.134
diff -u -p -r1.134 ping.c
--- ping.c      10 Nov 2015 18:36:33 -0000      1.134
+++ ping.c      28 Nov 2015 15:00:08 -0000
@@ -1004,7 +1004,7 @@ summary(int header, int insig)
                /* Only display average to microseconds */
                double num = nreceived + nrepeats;
                double avg = tsum / num;
-               double dev = sqrt(tsumsq / num - avg * avg);
+               double dev = sqrt(fmax(0, tsumsq / num - avg * avg));
                snprintf(buft, sizeof(buft),
                    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
                    tmin, avg, tmax, dev);
Index: ping6.c
===================================================================
RCS file: /cvs/src/sbin/ping6/ping6.c,v
retrieving revision 1.141
diff -u -p -r1.141 ping6.c
--- ping6.c     10 Nov 2015 18:36:33 -0000      1.141
+++ ping6.c     28 Nov 2015 15:03:31 -0000
@@ -1263,7 +1263,7 @@ summary(int signo)
                /* Only display average to microseconds */
                double num = nreceived + nrepeats;
                double avg = tsum / num;
-               double dev = sqrt(tsumsq / num - avg * avg);
+               double dev = sqrt(fmax(0, tsumsq / num - avg * avg));
                snprintf(buft, sizeof(buft),
                    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
                    tmin, avg, tmax, dev);

Reply via email to