From 356e8c85460dfa9747638719ee80976c2b8d1c9a Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 12 May 2024 12:04:09 +0100 Subject: [PATCH] [FEC] Fixed issues found by cppcheck --- src/utils/FEC.cpp | 28 ++++++++++++++++++++++------ src/utils/FEC.h | 25 +++++++++++++++---------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/utils/FEC.cpp b/src/utils/FEC.cpp index 39d7f5d5..515014d0 100644 --- a/src/utils/FEC.cpp +++ b/src/utils/FEC.cpp @@ -5,6 +5,14 @@ RadioLibBCH::RadioLibBCH() { } +RadioLibBCH::~RadioLibBCH() { + #if !RADIOLIB_STATIC_ONLY + delete[] this->alphaTo; + delete[] this->indexOf; + delete[] this->generator; + #endif +} + /* BCH Encoder based on https://www.codeproject.com/articles/13189/pocsag-encoder @@ -116,12 +124,15 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { // Search for roots 1, 2, ..., m-1 in cycle sets int32_t rdncy = 0; #if RADIOLIB_STATIC_ONLY - int32_t min[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + int32_t min[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1] = { 0 }; #else int32_t* min = new int32_t[this->n - this->k + 1]; #endif kaux = 0; + // ensure the first element is always initializer + min[0] = 0; + for(ii = 1; ii <= jj; ii++) { min[kaux] = 0; for(jj = 0; jj < size[ii]; jj++) { @@ -140,12 +151,15 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { int32_t noterms = kaux; #if RADIOLIB_STATIC_ONLY - int32_t zeros[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + int32_t zeros[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1] = { 0 }; #else int32_t* zeros = new int32_t[this->n - this->k + 1]; #endif kaux = 1; + // ensure the first element is always initializer + zeros[1] = 0; + for(ii = 0; ii < noterms; ii++) { for(jj = 0; jj < size[min[ii]]; jj++) { zeros[kaux] = cycle[min[ii]][jj]; @@ -186,9 +200,10 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { uint32_t RadioLibBCH::encode(uint32_t dataword) { // we only use the "k" most significant bits #if RADIOLIB_STATIC_ONLY - int32_t data[RADIOLIB_BCH_MAX_K]; + int32_t data[RADIOLIB_BCH_MAX_K] = { 0 }; #else int32_t* data = new int32_t[this->k]; + memset(data, 0, this->k*sizeof(int32_t)); #endif int32_t j1 = 0; for(int32_t i = this->n; i > (this->n - this->k); i--) { @@ -201,11 +216,11 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { // reset the M(x)+r array elements #if RADIOLIB_STATIC_ONLY - int32_t Mr[RADIOLIB_BCH_MAX_N]; + int32_t Mr[RADIOLIB_BCH_MAX_N] = { 0 }; #else int32_t* Mr = new int32_t[this->n]; + memset(Mr, 0x00, this->n*sizeof(int32_t)); #endif - memset(Mr, 0x00, this->n*sizeof(int32_t)); // copy the contents of data into Mr and add the zeros memcpy(Mr, data, this->k*sizeof(int32_t)); @@ -228,9 +243,10 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { } #if RADIOLIB_STATIC_ONLY - int32_t bb[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + int32_t bb[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1] = { 0 }; #else int32_t* bb = new int32_t[this->n - this->k + 1]; + memset(bb, 0, (this->n - this->k + 1)*sizeof(int32_t)); #endif j = 0; for(int32_t i = start; i < end; ++i) { diff --git a/src/utils/FEC.h b/src/utils/FEC.h index 0716fe11..30ba17ef 100644 --- a/src/utils/FEC.h +++ b/src/utils/FEC.h @@ -30,6 +30,11 @@ class RadioLibBCH { */ RadioLibBCH(); + /*! + \brief Default detructor. + */ + ~RadioLibBCH(); + /*! \brief Initialization method. \param n Code word length in bits, up to 32. @@ -47,19 +52,19 @@ class RadioLibBCH { uint32_t encode(uint32_t dataword); private: - uint8_t n; - uint8_t k; - uint32_t poly; - uint8_t m; + uint8_t n = 0; + uint8_t k = 0; + uint32_t poly = 0; + uint8_t m = 0; #if RADIOLIB_STATIC_ONLY - int32_t alphaTo[RADIOLIB_BCH_MAX_N + 1]; - int32_t indexOf[RADIOLIB_BCH_MAX_N + 1]; - int32_t generator[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + int32_t alphaTo[RADIOLIB_BCH_MAX_N + 1] = { 0 }; + int32_t indexOf[RADIOLIB_BCH_MAX_N + 1] = { 0 }; + int32_t generator[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1] = { 0 }; #else - int32_t* alphaTo; - int32_t* indexOf; - int32_t* generator; + int32_t* alphaTo = nullptr; + int32_t* indexOf = nullptr; + int32_t* generator = nullptr; #endif };