Fixed the overflow in the AGC

BUG=webrtc:8236

Review-Url: https://codereview.webrtc.org/3009373002
Cr-Original-Commit-Position: refs/heads/master@{#19818}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: fb2fa3f54e97c4465a6fd6fd992903a52397d739
diff --git a/modules/audio_processing/agc/legacy/digital_agc.c b/modules/audio_processing/agc/legacy/digital_agc.c
index a9ad55a..7f59785 100644
--- a/modules/audio_processing/agc/legacy/digital_agc.c
+++ b/modules/audio_processing/agc/legacy/digital_agc.c
@@ -524,8 +524,17 @@
     // iterate over samples
     for (n = 0; n < L; n++) {
       for (i = 0; i < num_bands; ++i) {
-        tmp32 = out[i][k * L + n] * (gain32 >> 4);
-        out[i][k * L + n] = (int16_t)(tmp32 >> 16);
+        int64_t tmp64 = ((int64_t)(out[i][k * L + n])) * (gain32 >> 4);
+        tmp64 = tmp64 >> 16;
+        if (tmp64 > 32767) {
+          out[i][k * L + n] = 32767;
+        }
+        else if (tmp64 < -32768) {
+          out[i][k * L + n] = -32768;
+        }
+        else {
+          out[i][k * L + n] = (int16_t)(tmp64);
+        }
       }
       gain32 += delta;
     }