From 03e03e9c11f5731c225d6cb99a1f36d4d8b699f6 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Wed, 6 Jul 2016 09:12:51 +0100 Subject: [PATCH] libdm: test for zero interval_ns in _utilization() (Coverity) It's possible for interval_ns to be zero if the interval is not set or the clock is misconfigured. Test for this before using the value as the divisor in the utilisation calculation. --- libdm/libdm-stats.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libdm/libdm-stats.c b/libdm/libdm-stats.c index 057bbcc7b..c0ef38f4c 100644 --- a/libdm/libdm-stats.c +++ b/libdm/libdm-stats.c @@ -2656,10 +2656,19 @@ static int _utilization(const struct dm_stats *dms, double *util, * for the last interval; do not allow a value > 100% utilization * to be passed to a dm_make_percent() call. We expect to see these * at startup if counters have not been cleared before the first read. + * + * A zero interval_ns is also an error since metrics cannot be + * calculated without a defined interval - return zero and emit a + * backtrace in this case. */ io_nsecs = dm_stats_get_counter(dms, DM_STATS_IO_NSECS, region_id, area_id); + if (!interval_ns) { + *util = 0.0; + return_0; + } + io_nsecs = ((io_nsecs < interval_ns) ? io_nsecs : interval_ns); *util = (double) io_nsecs / (double) interval_ns;