]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix a division by zero bug in isc_histo
authorTony Finch <fanf@isc.org>
Wed, 5 Apr 2023 13:17:29 +0000 (14:17 +0100)
committerOndřej Surý <ondrej@isc.org>
Wed, 5 Apr 2023 21:29:21 +0000 (23:29 +0200)
This can occur when calculating the standard deviation of an empty
histogram.

lib/isc/histo.c
tests/isc/histo_test.c

index b7e1b23fdf623aea00e56360d46948164c3e97fa..395b27b6b612de15f983ffb6be388aafe181af1e 100644 (file)
@@ -474,7 +474,7 @@ isc_histo_moments(const isc_histo_t *hg, double *pm0, double *pm1,
 
        OUTARG(pm0, pop);
        OUTARG(pm1, mean);
-       OUTARG(pm2, sqrt(sigma / pop));
+       OUTARG(pm2, (pop > 0) ? sqrt(sigma / pop) : 0.0);
 }
 
 /*
index 79224170b9553956a43ff164fd8ebd72964f6be3..f94cb52424fca888bc0038077a939af34e3efb4f 100644 (file)
@@ -146,7 +146,7 @@ ISC_RUN_TEST_IMPL(quantiles) {
        for (uint bits = ISC_HISTO_MINBITS; bits <= ISC_HISTO_MAXBITS; bits++) {
                isc_result_t result;
                uint64_t min, max, count;
-               double pop;
+               double pop, mean, sd;
                uint key;
 
                isc_nanosecs_t start = isc_time_monotonic();
@@ -154,6 +154,12 @@ ISC_RUN_TEST_IMPL(quantiles) {
                isc_histo_t *hg = NULL;
                isc_histo_create(mctx, bits, &hg);
 
+               /* ensure empty histogram does not divide by zero */
+               isc_histo_moments(hg, &pop, &mean, &sd);
+               assert_true(pop == 0.0);
+               assert_true(mean == 0.0);
+               assert_true(sd == 0.0);
+
                for (key = 0; isc_histo_get(hg, key, &min, &max, &count) ==
                              ISC_R_SUCCESS;
                     key++)