aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Luis Duran <jlduran@gmail.com>2023-10-06 17:55:06 +0000
committerMark Johnston <markj@FreeBSD.org>2023-11-13 15:20:50 +0000
commit29667c6fcc36de5cf28942bd72e5b5f5926d19f5 (patch)
treedd2c479cb9e4bae7cce08864be2e6a8933a39379
parent221a60a426d7f73dc4ef021821bf81dd269ba943 (diff)
downloadsrc-29667c6fcc36de5cf28942bd72e5b5f5926d19f5.tar.gz
src-29667c6fcc36de5cf28942bd72e5b5f5926d19f5.zip
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)
-rw-r--r--sbin/ping/ping.c4
-rw-r--r--sbin/ping/ping6.c4
2 files changed, 4 insertions, 4 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index f248a0eb738c..e121ebdb19e2 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -1521,10 +1521,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 432a59bf1303..0edeb01b2dc0 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);