From 3b8bc35bd9876fbdcfa285b102d4f8ef2a5afb27 Mon Sep 17 00:00:00 2001 From: Steve Underwood Date: Tue, 22 Jul 2014 10:51:42 +0800 Subject: [PATCH] More fixes for Coverity issues --- .../src/spandsp/private/super_tone_tx.h | 10 +++---- libs/spandsp/src/spandsp/super_tone_tx.h | 3 ++ libs/spandsp/src/spandsp/vector_int.h | 18 ++++++++++++ libs/spandsp/src/super_tone_tx.c | 17 +++++++---- libs/spandsp/src/time_scale.c | 29 ++++++++++--------- .../tests/t81_t82_arith_coding_tests.c | 2 +- 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/libs/spandsp/src/spandsp/private/super_tone_tx.h b/libs/spandsp/src/spandsp/private/super_tone_tx.h index b6efced965..cec758026d 100644 --- a/libs/spandsp/src/spandsp/private/super_tone_tx.h +++ b/libs/spandsp/src/spandsp/private/super_tone_tx.h @@ -28,7 +28,7 @@ struct super_tone_tx_step_s { - tone_gen_tone_descriptor_t tone[4]; + tone_gen_tone_descriptor_t tone[SUPER_TONE_TX_MAX_TONES]; int tone_on; int length; int cycles; @@ -38,12 +38,12 @@ struct super_tone_tx_step_s struct super_tone_tx_state_s { - tone_gen_tone_descriptor_t tone[4]; - uint32_t phase[4]; + tone_gen_tone_descriptor_t tone[SUPER_TONE_TX_MAX_TONES]; + uint32_t phase[SUPER_TONE_TX_MAX_TONES]; int current_position; int level; - super_tone_tx_step_t *levels[4]; - int cycles[4]; + super_tone_tx_step_t *levels[SUPER_TONE_TX_MAX_LEVELS]; + int cycles[SUPER_TONE_TX_MAX_LEVELS]; }; #endif diff --git a/libs/spandsp/src/spandsp/super_tone_tx.h b/libs/spandsp/src/spandsp/super_tone_tx.h index 55b8d670f0..879319ae47 100644 --- a/libs/spandsp/src/spandsp/super_tone_tx.h +++ b/libs/spandsp/src/spandsp/super_tone_tx.h @@ -39,6 +39,9 @@ complex cadence patterns. */ +#define SUPER_TONE_TX_MAX_LEVELS 4 +#define SUPER_TONE_TX_MAX_TONES 4 + typedef struct super_tone_tx_step_s super_tone_tx_step_t; typedef struct super_tone_tx_state_s super_tone_tx_state_t; diff --git a/libs/spandsp/src/spandsp/vector_int.h b/libs/spandsp/src/spandsp/vector_int.h index daec30670e..775d525c86 100644 --- a/libs/spandsp/src/spandsp/vector_int.h +++ b/libs/spandsp/src/spandsp/vector_int.h @@ -49,6 +49,24 @@ static __inline__ void vec_copyi32(int32_t z[], const int32_t x[], int n) } /*- End of function --------------------------------------------------------*/ +static __inline__ void vec_movei(int z[], const int x[], int n) +{ + memmove(z, x, n*sizeof(z[0])); +} +/*- End of function --------------------------------------------------------*/ + +static __inline__ void vec_movei16(int16_t z[], const int16_t x[], int n) +{ + memmove(z, x, n*sizeof(z[0])); +} +/*- End of function --------------------------------------------------------*/ + +static __inline__ void vec_movei32(int32_t z[], const int32_t x[], int n) +{ + memmove(z, x, n*sizeof(z[0])); +} +/*- End of function --------------------------------------------------------*/ + static __inline__ void vec_zeroi(int z[], int n) { memset(z, 0, n*sizeof(z[0])); diff --git a/libs/spandsp/src/super_tone_tx.c b/libs/spandsp/src/super_tone_tx.c index 5b8ab4303d..cd6ac8290e 100644 --- a/libs/spandsp/src/super_tone_tx.c +++ b/libs/spandsp/src/super_tone_tx.c @@ -170,7 +170,7 @@ SPAN_DECLARE(int) super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max float xamp; super_tone_tx_step_t *tree; - if (s->level < 0 || s->level > 3) + if (s->level < 0 || s->level >= SUPER_TONE_TX_MAX_LEVELS) return 0; samples = 0; tree = s->levels[s->level]; @@ -182,7 +182,7 @@ SPAN_DECLARE(int) super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max if (s->current_position == 0) { /* New step - prepare the tone generator */ - for (i = 0; i < 4; i++) + for (i = 0; i < SUPER_TONE_TX_MAX_TONES; i++) s->tone[i] = tree->tone[i]; } len = tree->length - s->current_position; @@ -216,7 +216,7 @@ SPAN_DECLARE(int) super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max for (limit = len + samples; samples < limit; samples++) { xamp = 0.0f; - for (i = 0; i < 4; i++) + for (i = 0; i < SUPER_TONE_TX_MAX_TONES; i++) { if (s->tone[i].phase_rate == 0) break; @@ -251,9 +251,14 @@ SPAN_DECLARE(int) super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max /* Nesting has priority... */ if (tree->nest) { - tree = tree->nest; - s->levels[++s->level] = tree; - s->cycles[s->level] = tree->cycles; + if (s->level < SUPER_TONE_TX_MAX_LEVELS - 1) + { + /* TODO: We have stopped an over-range value being used, but we really + ought to deal with the condition properly. */ + tree = tree->nest; + s->levels[++s->level] = tree; + s->cycles[s->level] = tree->cycles; + } } else { diff --git a/libs/spandsp/src/time_scale.c b/libs/spandsp/src/time_scale.c index c8b6b23825..5337e4ad0c 100644 --- a/libs/spandsp/src/time_scale.c +++ b/libs/spandsp/src/time_scale.c @@ -52,8 +52,9 @@ #include "spandsp/telephony.h" #include "spandsp/alloc.h" #include "spandsp/fast_convert.h" -#include "spandsp/time_scale.h" +#include "spandsp/vector_int.h" #include "spandsp/saturated.h" +#include "spandsp/time_scale.h" #include "spandsp/private/time_scale.h" @@ -190,46 +191,46 @@ SPAN_DECLARE(int) time_scale(time_scale_state_t *s, int16_t out[], int16_t in[], if (s->fill + len < s->buf_len) { /* Cannot continue without more samples */ - memcpy(&s->buf[s->fill], in, sizeof(int16_t)*len); + vec_copyi16(&s->buf[s->fill], in, len); s->fill += len; return out_len; } k = s->buf_len - s->fill; - memcpy(&s->buf[s->fill], in, sizeof(int16_t)*k); + vec_copyi16(&s->buf[s->fill], in, k); in_len += k; s->fill = s->buf_len; while (s->fill == s->buf_len) { while (s->lcp >= s->buf_len) { - memcpy(&out[out_len], s->buf, sizeof(int16_t)*s->buf_len); + vec_copyi16(&out[out_len], s->buf, s->buf_len); out_len += s->buf_len; if (len - in_len < s->buf_len) { /* Cannot continue without more samples */ - memcpy(s->buf, &in[in_len], sizeof(int16_t)*(len - in_len)); + vec_copyi16(s->buf, &in[in_len], len - in_len); s->fill = len - in_len; s->lcp -= s->buf_len; return out_len; } - memcpy(s->buf, &in[in_len], sizeof(int16_t)*s->buf_len); + vec_copyi16(s->buf, &in[in_len], s->buf_len); in_len += s->buf_len; s->lcp -= s->buf_len; } if (s->lcp > 0) { - memcpy(&out[out_len], s->buf, sizeof(int16_t)*s->lcp); + vec_copyi16(&out[out_len], s->buf, s->lcp); out_len += s->lcp; - memmove(s->buf, &s->buf[s->lcp], sizeof(int16_t)*(s->buf_len - s->lcp)); + vec_movei16(s->buf, &s->buf[s->lcp], s->buf_len - s->lcp); if (len - in_len < s->lcp) { /* Cannot continue without more samples */ - memcpy(&s->buf[s->buf_len - s->lcp], &in[in_len], sizeof(int16_t)*(len - in_len)); + vec_copyi16(&s->buf[s->buf_len - s->lcp], &in[in_len], len - in_len); s->fill = s->buf_len - s->lcp + len - in_len; s->lcp = 0; return out_len; } - memcpy(&s->buf[s->buf_len - s->lcp], &in[in_len], sizeof(int16_t)*s->lcp); + vec_copyi16(&s->buf[s->buf_len - s->lcp], &in[in_len], s->lcp); in_len += s->lcp; s->lcp = 0; } @@ -259,21 +260,21 @@ SPAN_DECLARE(int) time_scale(time_scale_state_t *s, int16_t out[], int16_t in[], { /* Speed up - drop a chunk of data */ overlap_add(s->buf, &s->buf[pitch], pitch); - memcpy(&s->buf[pitch], &s->buf[2*pitch], sizeof(int16_t)*(s->buf_len - 2*pitch)); + vec_copyi16(&s->buf[pitch], &s->buf[2*pitch], s->buf_len - 2*pitch); if (len - in_len < pitch) { /* Cannot continue without more samples */ - memcpy(&s->buf[s->buf_len - pitch], &in[in_len], sizeof(int16_t)*(len - in_len)); + vec_copyi16(&s->buf[s->buf_len - pitch], &in[in_len], len - in_len); s->fill += (len - in_len - pitch); return out_len; } - memcpy(&s->buf[s->buf_len - pitch], &in[in_len], sizeof(int16_t)*pitch); + vec_copyi16(&s->buf[s->buf_len - pitch], &in[in_len], pitch); in_len += pitch; } else { /* Slow down - insert a chunk of data */ - memcpy(&out[out_len], s->buf, sizeof(int16_t)*pitch); + vec_copyi16(&out[out_len], s->buf, pitch); out_len += pitch; overlap_add(&s->buf[pitch], s->buf, pitch); } diff --git a/libs/spandsp/tests/t81_t82_arith_coding_tests.c b/libs/spandsp/tests/t81_t82_arith_coding_tests.c index 84a61a5df1..7df59567ea 100644 --- a/libs/spandsp/tests/t81_t82_arith_coding_tests.c +++ b/libs/spandsp/tests/t81_t82_arith_coding_tests.c @@ -226,7 +226,7 @@ int main(int argc, char *argv[]) } printf("Test passed\n"); t81_t82_arith_encode_free(se); - t81_t82_arith_encode_free(sd); + t81_t82_arith_decode_free(sd); printf("Tests passed\n"); return 0; }