From 9e940b0b5aa9bfb7e23cae41ae6693a532913547 Mon Sep 17 00:00:00 2001 From: wb2osz Date: Sun, 30 Jul 2017 21:43:29 -0400 Subject: [PATCH] Issue 107 - Avoid Signed integer overflows. --- demod_9600.c | 4 +++- demod_afsk.c | 4 +++- demod_psk.c | 3 ++- gen_packets.c | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/demod_9600.c b/demod_9600.c index 58c4c04..e94f8aa 100644 --- a/demod_9600.c +++ b/demod_9600.c @@ -491,7 +491,9 @@ inline static void nudge_pll (int chan, int subchan, int slice, float demod_out_ */ D->slicer[slice].prev_d_c_pll = D->slicer[slice].data_clock_pll; - D->slicer[slice].data_clock_pll += D->pll_step_per_sample; + + // Perform the add as unsigned to avoid signed overflow error. + D->slicer[slice].data_clock_pll = (signed)((unsigned)(D->slicer[slice].data_clock_pll) + (unsigned)(D->pll_step_per_sample)); if ( D->slicer[slice].prev_d_c_pll > 1000000000 && D->slicer[slice].data_clock_pll < -1000000000) { diff --git a/demod_afsk.c b/demod_afsk.c index e8252db..80bd40e 100644 --- a/demod_afsk.c +++ b/demod_afsk.c @@ -1122,7 +1122,9 @@ inline static void nudge_pll (int chan, int subchan, int slice, int demod_data, */ D->slicer[slice].prev_d_c_pll = D->slicer[slice].data_clock_pll; - D->slicer[slice].data_clock_pll += D->pll_step_per_sample; + + // Perform the add as unsigned to avoid signed overflow error. + D->slicer[slice].data_clock_pll = (signed)((unsigned)(D->slicer[slice].data_clock_pll) + (unsigned)(D->pll_step_per_sample)); //text_color_set(DW_COLOR_DEBUG); // dw_printf ("prev = %lx, new data clock pll = %lx\n" D->prev_d_c_pll, D->data_clock_pll); diff --git a/demod_psk.c b/demod_psk.c index db9407a..ee660ef 100644 --- a/demod_psk.c +++ b/demod_psk.c @@ -794,7 +794,8 @@ inline static void nudge_pll (int chan, int subchan, int slice, int demod_bits, D->slicer[slice].prev_d_c_pll = D->slicer[slice].data_clock_pll; - D->slicer[slice].data_clock_pll += D->pll_step_per_sample; + // Perform the add as unsigned to avoid signed overflow error. + D->slicer[slice].data_clock_pll = (signed)((unsigned)(D->slicer[slice].data_clock_pll) + (unsigned)(D->pll_step_per_sample)); if (D->slicer[slice].data_clock_pll < 0 && D->slicer[slice].prev_d_c_pll >= 0) { diff --git a/gen_packets.c b/gen_packets.c index 4a8e8c9..2829ecf 100644 --- a/gen_packets.c +++ b/gen_packets.c @@ -84,7 +84,8 @@ static int seed = 1; static int my_rand (void) { - seed = ((seed * 1103515245) + 12345) & MY_RAND_MAX; + // Perform the calculation as unsigned to avoid signed overflow error. + seed = (int)(((unsigned)seed * 1103515245) + 12345) & MY_RAND_MAX; return (seed); }