From 19d1caa483924943f041a598b2434320fcba4b05 Mon Sep 17 00:00:00 2001 From: Patryk Obara Date: Sun, 1 Mar 2020 03:38:11 +0100 Subject: [PATCH] Avoid division by zero when initializing pcspeaker Coverity detects possible division by zero in calculation of spkr.min_tr few lines below; this is a false-positive issue detected by Coverity, but only bacause int value passed by user has a set of pre-determined values. We can as well make sure that value is never going to be smaller than the minimum allowed. --- src/hardware/pcspeaker.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hardware/pcspeaker.cpp b/src/hardware/pcspeaker.cpp index 9de948ef..172198f6 100644 --- a/src/hardware/pcspeaker.cpp +++ b/src/hardware/pcspeaker.cpp @@ -16,15 +16,15 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include -#include "dosbox.h" #include "mixer.h" + +#include +#include + #include "timer.h" #include "setup.h" #include "pic.h" - #ifndef PI #define PI 3.14159265358979323846 #endif @@ -331,14 +331,14 @@ public: spkr.mode=SPKR_OFF; spkr.last_ticks=0; spkr.last_index=0; - spkr.rate=section->Get_int("pcrate"); + spkr.rate = std::max(section->Get_int("pcrate"), 8000); spkr.pit_mode=3; spkr.pit_max=(1000.0f/PIT_TICK_RATE)*1320; spkr.pit_half=spkr.pit_max/2; spkr.pit_new_max=spkr.pit_max; spkr.pit_new_half=spkr.pit_half; spkr.pit_index=0; - spkr.min_tr=(PIT_TICK_RATE+spkr.rate/2-1)/(spkr.rate/2); + spkr.min_tr = (PIT_TICK_RATE + spkr.rate/2 - 1) / (spkr.rate / 2); spkr.used=0; /* Register the sound channel */ spkr.chan=MixerChan.Install(&PCSPEAKER_CallBack,spkr.rate,"SPKR");