git: bf7f8a4e60ce - stable/14 - ping: Avoid reporting NaNs

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 06 Nov 2023 16:45:49 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=bf7f8a4e60ce450bf31e6ae110e6e37c85ee5eb0

commit bf7f8a4e60ce450bf31e6ae110e6e37c85ee5eb0
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2023-10-06 17:55:06 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-11-06 16:45:43 +0000

    ping: Avoid reporting NaNs
    
    Avoid calculating the square root of negative zero, which can easily
    happen on certain architectures when calculating the population standard
    deviation with a sample size of one, e.g., 0.01 - (0.1 * 0.1) =
    -0.000000.
    
    Avoid returning a NaN by capping the minimum possible variance value to
    zero (positive).
    
    In the future, maybe skip reporting statistics at all for a single
    sample.
    
    Reported by:    Jenkins
    Reviewed by:    asomers
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/863
    Differential Revision:  https://reviews.freebsd.org/D42114
    
    (cherry picked from commit 4d348e83b738347f6aaf2b110459a01c5402d04e)
---
 sbin/ping/ping.c  | 4 ++--
 sbin/ping/ping6.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index 072d4607f745..f67d85f30a5d 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -1522,10 +1522,10 @@ finish(void)
 	if (nreceived && timing) {
 		double n = nreceived + nrepeats;
 		double avg = tsum / n;
-		double vari = tsumsq / n - avg * avg;
+		double stddev = sqrt(fmax(0, tsumsq / n - avg * avg));
 		(void)printf(
 		    "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
-		    tmin, avg, tmax, sqrt(vari));
+		    tmin, avg, tmax, stddev);
 	}
 
 	if (nreceived)
diff --git a/sbin/ping/ping6.c b/sbin/ping/ping6.c
index bd1658f9500a..d14da9c67a52 100644
--- a/sbin/ping/ping6.c
+++ b/sbin/ping/ping6.c
@@ -2349,10 +2349,10 @@ summary(void)
 		/* Only display average to microseconds */
 		double num = nreceived + nrepeats;
 		double avg = tsum / num;
-		double dev = sqrt(tsumsq / num - avg * avg);
+		double stddev = sqrt(fmax(0, tsumsq / num - avg * avg));
 		(void)printf(
 		    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
-		    tmin, avg, tmax, dev);
+		    tmin, avg, tmax, stddev);
 		(void)fflush(stdout);
 	}
 	(void)fflush(stdout);