Fix a division by zero bug in isc_histo

This can occur when calculating the standard deviation of an empty
histogram.
This commit is contained in:
Tony Finch
2023-04-05 14:17:29 +01:00
committed by Ondřej Surý
parent 426eb4ffd2
commit 3405b43fe9
2 changed files with 8 additions and 2 deletions

View 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);
}
/*

View 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++)