From a50eecf65b5ce5d4f03768c5c2cb4b492d2badad Mon Sep 17 00:00:00 2001
From: Martin Storsjo <martin@martin.st>
Date: Fri, 4 May 2018 12:46:44 +0300
Subject: [PATCH] Fix overflows in accumulation, fixing crashes

This fixes github issue #83.
---
 libSBRenc/src/tran_det.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libSBRenc/src/tran_det.cpp b/libSBRenc/src/tran_det.cpp
index 0e35ec3..51d6efe 100644
--- a/libSBRenc/src/tran_det.cpp
+++ b/libSBRenc/src/tran_det.cpp
@@ -256,12 +256,12 @@ static FIXP_DBL addLowbandEnergies(FIXP_DBL **Energies,
   /* freqBandTable[LORES] has MAX_FREQ_COEFFS/2 +1 coeefs max. */
   for (ts=tran_offdiv2; ts<YBufferWriteOffset; ts++) {
     for (k = 0; k < freqBandTable[0]; k++) {
-      accu1 += Energies[ts][k] >> 6;
+      accu1 = fAddSaturate(accu1, Energies[ts][k] >> 6);
     }
   }
   for (; ts<tran_offdiv2+(slots>>nrgSzShift); ts++) {
     for (k = 0; k < freqBandTable[0]; k++) {
-      accu2 += Energies[ts][k] >> 9;
+      accu2 = fAddSaturate(accu2, Energies[ts][k] >> 9);
     }
   }
 
