[automerger] Add some error/overflow checks in codebook handling am: 6eb63e1192 am: bc4bde3bee am: 632704bab3 am: 9cea7b942e am: 57035e3928

Change-Id: I43a1ac97a52b8c3cb6df55efde357e3a5ba2889d
diff --git a/Tremolo/codebook.c b/Tremolo/codebook.c
index 467640d..a06302d 100644
--- a/Tremolo/codebook.c
+++ b/Tremolo/codebook.c
@@ -376,14 +376,21 @@
     long acc=1;
     long acc1=1;
     int i;
-    for(i=0;i<b->dim;i++){
-      acc*=vals;
-      acc1*=vals+1;
+    for (i = 0; i < b->dim; i++) {
+      if (acc > b->entries / vals) {
+          break;
+      }
+      acc *= vals;
+      if (acc1 > LONG_MAX / (vals + 1)) {
+        acc1 = LONG_MAX;
+      } else {
+        acc1 *= (vals + 1);
+      }
     }
-    if(acc<=b->entries && acc1>b->entries){
+    if (i >= b->dim && acc <= b->entries && acc1 > b->entries) {
       return(vals);
     }else{
-      if(acc>b->entries){
+      if (i < b->dim || acc > b->entries) {
         vals--;
       }else{
         vals++;