From a581e0d1806248cae779b9d4b1472a293c166de0 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 11 Jun 2013 11:04:51 +0200 Subject: [PATCH] Avoid division by zero in audio envelope processing. Fixes sound FX on android in some games. --- Core/HW/SasAudio.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Core/HW/SasAudio.cpp b/Core/HW/SasAudio.cpp index ed53105419..37c001a96a 100644 --- a/Core/HW/SasAudio.cpp +++ b/Core/HW/SasAudio.cpp @@ -646,9 +646,15 @@ static int durationFromRate(int rate) const short expCurveReference = 0x7000; +// This needs a rewrite / rethink. Doing all this per sample is insane. static int getExpCurveAt(int index, int duration) { const short curveLength = sizeof(expCurve) / sizeof(short); + if (duration == 0) { + // Avoid division by zero, and thus undefined behaviour in conversion to int. + return 0; + } + float curveIndex = (index * curveLength) / (float) duration; int curveIndex1 = (int) curveIndex; int curveIndex2 = curveIndex1 + 1;