Blob Blame History Raw
From b3df98430688d5a55e3555aafa2ae8d8a57976d6 Mon Sep 17 00:00:00 2001
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
Date: Mon, 15 Sep 2014 02:04:31 +0900
Subject: [PATCH 1001/1005] analogtv.c/fastrnd: wrap signed integer correctly

gcc49 sanitizer shows the errors like below:
../../hacks/analogtv.c:1201:22: runtime error: signed integer overflow: -1118478881 + -2147483647 cannot be represented in type 'int [262]'
../../hacks/analogtv.c:1205:24: runtime error: signed integer overflow: -1157379731 + -2147483647 cannot be represented in type 'int [262]'
../../hacks/analogtv.c:1246:32: runtime error: signed integer overflow: -1015960810 + -2147483647 cannot be represented in type 'int [262]'

These all come from the calculation "((int)fastrnd-(int)0x7fffffff)". Wrap this
signed integer calculation correctly.
---
 hacks/analogtv.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/hacks/analogtv.c b/hacks/analogtv.c
index 7c2993e..009d7bc 100644
--- a/hacks/analogtv.c
+++ b/hacks/analogtv.c
@@ -65,6 +65,7 @@
 # include <X11/Xlib.h>
 # include <X11/Xutil.h>
 #endif
+#include <limits.h>
 
 #include <assert.h>
 #include <errno.h>
@@ -1195,14 +1196,17 @@ static void analogtv_init_signal(const analogtv *it, double noiselevel, unsigned
   float *pe=it->rx_signal + end;
   float *p=ps;
   unsigned int fastrnd=rnd_seek(FASTRND_A, FASTRND_C, it->random0, start);
+  unsigned int fastrnd_offset;
   float nm1,nm2;
   float noisemul = sqrt(noiselevel*150)/(float)0x7fffffff;
 
-  nm1 = ((int)fastrnd-(int)0x7fffffff) * noisemul;
+  fastrnd_offset = fastrnd - 0x7fffffff;
+  nm1 = (fastrnd_offset <= INT_MAX ? (int)fastrnd_offset : -1 - (int)(UINT_MAX - fastrnd_offset)) * noisemul;
   while (p != pe) {
     nm2=nm1;
     fastrnd = (fastrnd*FASTRND_A+FASTRND_C) & 0xffffffffu;
-    nm1 = ((int)fastrnd-(int)0x7fffffff) * noisemul;
+    fastrnd_offset = fastrnd - 0x7fffffff;
+    nm1 = (fastrnd_offset <= INT_MAX ? (int)fastrnd_offset : -1 - (int)(UINT_MAX - fastrnd_offset)) * noisemul;
     *p++ = nm1*nm2;
   }
 }
@@ -1243,7 +1247,8 @@ static void analogtv_add_signal(const analogtv *it, const analogtv_reception *re
     */
 
     float sig0=(float)s[0];
-    float noise = ((int)fastrnd-(int)0x7fffffff) * (50.0f/(float)0x7fffffff);
+    unsigned int fastrnd_offset = fastrnd - 0x7fffffff;
+    float noise = (fastrnd_offset <= INT_MAX ? (int)fastrnd_offset : -1 - (int)(UINT_MAX - fastrnd_offset)) * (50.0f/(float)0x7fffffff);
     fastrnd = (fastrnd*FASTRND_A+FASTRND_C) & 0xffffffffu;
 
     p[0] += sig0 * level * (1.0f - noise_ampl) + noise * noise_ampl;
-- 
2.1.0