From e4ad1a74716a72a2bf81645bff8d6cc08101456b Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 9 Oct 2019 23:01:07 +0200 Subject: [PATCH] coverity 227889 227898 - division by zero --- common/lfdemod.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/common/lfdemod.c b/common/lfdemod.c index 1d1749d8e..9ed1a83c3 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -117,8 +117,11 @@ void computeSignalProperties(uint8_t *samples, uint32_t size) { sum += samples[i]; cnt++; - } - signalprop.mean = sum / cnt; + } + if (cnt > 0) + signalprop.mean = sum / cnt; + else + signalprop.mean = 0; #else for (uint32_t i = SIGNAL_IGNORE_FIRST_SAMPLES; i < size; i++) { if (samples[i] < signalprop.low) signalprop.low = samples[i]; @@ -160,8 +163,11 @@ void removeSignalOffset(uint8_t *samples, uint32_t size) { acc_off += samples[i] - 128; cnt++; - } - acc_off /= cnt; + } + if (cnt > 0) + acc_off /= cnt; + else + acc_off = 0; #else for (uint32_t i = SIGNAL_IGNORE_FIRST_SAMPLES; i < size; i++) acc_off += samples[i] - 128;