From a2ab043c0d331ce597fb0b7ce6b48b7523248844 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Thu, 19 Sep 2019 16:20:21 +0200 Subject: [PATCH] fix memleaks and uninitialized vars found by cppcheck --- lib/examples/pdsch_ue.c | 4 ++-- lib/src/phy/io/filesource.c | 2 +- lib/src/phy/sync/test/nsss_test.c | 26 +++++++++++++-------- lib/src/phy/sync/test/sync_nbiot_test.c | 31 ++++++++++++++++--------- srsenb/src/stack/mac/scheduler.cc | 2 +- srsepc/src/main.cc | 4 ++-- 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/lib/examples/pdsch_ue.c b/lib/examples/pdsch_ue.c index 7c96e437c..c795bceef 100644 --- a/lib/examples/pdsch_ue.c +++ b/lib/examples/pdsch_ue.c @@ -606,13 +606,13 @@ int main(int argc, char **argv) { dl_sf.tdd_config.configured = true; } - srslte_chest_dl_cfg_t chest_pdsch_cfg; + srslte_chest_dl_cfg_t chest_pdsch_cfg = {}; chest_pdsch_cfg.cfo_estimate_enable = prog_args.enable_cfo_ref; chest_pdsch_cfg.cfo_estimate_sf_mask = 1023; chest_pdsch_cfg.interpolate_subframe = !prog_args.average_subframe; // Special configuration for MBSFN channel estimation - srslte_chest_dl_cfg_t chest_mbsfn_cfg; + srslte_chest_dl_cfg_t chest_mbsfn_cfg = {}; chest_mbsfn_cfg.filter_type = SRSLTE_CHEST_FILTER_TRIANGLE; chest_mbsfn_cfg.filter_coef[0] = 0.1; chest_mbsfn_cfg.interpolate_subframe = true; diff --git a/lib/src/phy/io/filesource.c b/lib/src/phy/io/filesource.c index 8cf6dad86..3f4f4ee8d 100644 --- a/lib/src/phy/io/filesource.c +++ b/lib/src/phy/io/filesource.c @@ -52,7 +52,7 @@ void srslte_filesource_seek(srslte_filesource_t *q, int pos) { int read_complex_f(FILE *f, _Complex float *y) { char in_str[64]; - _Complex float x; + _Complex float x = 0; if (NULL == fgets(in_str, 64, f)) { return -1; } else { diff --git a/lib/src/phy/sync/test/nsss_test.c b/lib/src/phy/sync/test/nsss_test.c index d2ad94c0e..514ca07fc 100644 --- a/lib/src/phy/sync/test/nsss_test.c +++ b/lib/src/phy/sync/test/nsss_test.c @@ -86,8 +86,8 @@ void parse_args(int argc, char** argv) int main(int argc, char** argv) { - cf_t* fft_buffer; - cf_t* buffer; + cf_t* fft_buffer = NULL; + cf_t* buffer = NULL; srslte_nsss_synch_t syncobj; srslte_ofdm_t ifft; int fft_size; @@ -107,20 +107,20 @@ int main(int argc, char** argv) fft_size = srslte_symbol_sz(NOF_PRB); if (fft_size < 0) { fprintf(stderr, "Invalid nof_prb=%d\n", NOF_PRB); - return ret; + goto exit; } printf("SFLEN is %d samples\n", SFLEN); fft_buffer = malloc(sizeof(cf_t) * SFLEN * max_num_sf); if (!fft_buffer) { perror("malloc"); - return ret; + goto exit; } memset(fft_buffer, 0, sizeof(cf_t) * SFLEN * max_num_sf); if (srslte_ofdm_tx_init(&ifft, SRSLTE_CP_NORM, buffer, fft_buffer, NOF_PRB)) { fprintf(stderr, "Error creating iFFT object\n"); - return ret; + goto exit; } if (input_file_name != NULL) { @@ -128,7 +128,7 @@ int main(int argc, char** argv) printf("Opening file %s\n", input_file_name); if (srslte_filesource_init(&fsrc, input_file_name, SRSLTE_COMPLEX_FLOAT_BIN)) { fprintf(stderr, "Error opening file %s\n", input_file_name); - return ret; + goto exit; } num_sf = 0; @@ -141,7 +141,7 @@ int main(int argc, char** argv) int n = srslte_filesource_read(&fsrc, &fft_buffer[num_sf * SFLEN], SFLEN); if (n < 0) { fprintf(stderr, "Error reading samples\n"); - return ret; + goto exit; } if (n < SFLEN) { fprintf(stdout, "End of file (n=%d, sflen=%d)\n", n, SFLEN); @@ -157,7 +157,7 @@ int main(int argc, char** argv) printf("Initializing NSSS synch with %dx%d samples.\n", num_sf, SFLEN); if (srslte_nsss_synch_init(&syncobj, num_sf * SFLEN, fft_size)) { fprintf(stderr, "Error initializing NSSS object\n"); - return ret; + goto exit; } // write single NSSS sequence if not reading from input file @@ -214,10 +214,16 @@ int main(int argc, char** argv) #endif // cleanup +exit: srslte_nsss_synch_free(&syncobj); - free(buffer); - free(fft_buffer); srslte_ofdm_tx_free(&ifft); + if (buffer) { + free(buffer); + } + if (fft_buffer) { + free(fft_buffer); + } + return ret; } diff --git a/lib/src/phy/sync/test/sync_nbiot_test.c b/lib/src/phy/sync/test/sync_nbiot_test.c index a77e98112..bf0a009fc 100644 --- a/lib/src/phy/sync/test/sync_nbiot_test.c +++ b/lib/src/phy/sync/test/sync_nbiot_test.c @@ -83,8 +83,9 @@ void parse_args(int argc, char** argv) int main(int argc, char** argv) { + int ret = SRSLTE_ERROR; int sf_idx = 0; - cf_t* fft_buffer; + cf_t* fft_buffer = NULL; _Complex float buffer[SFLEN]; // FLEN + fft_size srslte_filesource_t fsrc; @@ -102,7 +103,7 @@ int main(int argc, char** argv) printf("Opening file...\n"); if (srslte_filesource_init(&fsrc, input_file_name, SRSLTE_COMPLEX_FLOAT_BIN)) { fprintf(stderr, "Error opening file %s\n", input_file_name); - exit(-1); + goto exit; } } @@ -116,14 +117,14 @@ int main(int argc, char** argv) fft_buffer = malloc(sizeof(cf_t) * SFLEN * 2); if (!fft_buffer) { perror("malloc"); - exit(-1); + goto exit; } memset(buffer, 0, sizeof(cf_t) * SFLEN); if (srslte_cfo_init(&cfocorr, SFLEN)) { fprintf(stderr, "Error initiating CFO\n"); - return -1; + goto exit; } // Set a CFO tolerance of approx 100 Hz srslte_cfo_set_tol(&cfocorr, 100.0 / (15000.0 * fft_size)); @@ -131,7 +132,7 @@ int main(int argc, char** argv) // init synch object for a maximum SFLEN samples if (srslte_sync_nbiot_init(&syncobj, SFLEN, SFLEN, fft_size)) { fprintf(stderr, "Error initiating NPSS/NSSS\n"); - return -1; + goto exit; } srslte_sync_nbiot_set_cfo_enable(&syncobj, true); @@ -140,7 +141,7 @@ int main(int argc, char** argv) if (srslte_ofdm_tx_init( &ifft, SRSLTE_CP_NORM, buffer, &fft_buffer[offset], SRSLTE_NBIOT_DEFAULT_NUM_PRB_BASECELL)) { fprintf(stderr, "Error creating iFFT object\n"); - exit(-1); + goto exit; } srslte_ofdm_set_normalize(&ifft, true); srslte_ofdm_set_freq_shift(&ifft, -SRSLTE_NBIOT_FREQ_SHIFT_FACTOR); @@ -162,7 +163,7 @@ int main(int argc, char** argv) int n = srslte_filesource_read(&fsrc, fft_buffer, SFLEN); if (n < 0) { fprintf(stderr, "Error reading samples\n"); - exit(-1); + goto exit; } } @@ -204,15 +205,23 @@ int main(int argc, char** argv) (SFLEN + SRSLTE_NPSS_CORR_FILTER_LEN - 1) * sizeof(float)); #endif + printf("Ok\n"); + + ret = SRSLTE_SUCCESS; + // cleanup - if (input_file_name != NULL) +exit: + if (input_file_name != NULL) { srslte_filesource_free(&fsrc); + } srslte_sync_nbiot_free(&syncobj); - free(fft_buffer); srslte_cfo_free(&cfocorr); - printf("Ok\n"); - exit(0); + if (fft_buffer) { + free(fft_buffer); + } + + return ret; } void write_to_file() diff --git a/srsenb/src/stack/mac/scheduler.cc b/srsenb/src/stack/mac/scheduler.cc index e5c02fbf9..95a3bdbbb 100644 --- a/srsenb/src/stack/mac/scheduler.cc +++ b/srsenb/src/stack/mac/scheduler.cc @@ -214,7 +214,7 @@ alloc_outcome_t sched::tti_sched_t::alloc_ul(sched_ue* use return ret; } - ul_alloc_t ul_alloc; + ul_alloc_t ul_alloc = {}; ul_alloc.type = alloc_type; ul_alloc.dci_idx = tti_alloc.get_pdcch_grid().nof_allocs() - 1; ul_alloc.user_ptr = user; diff --git a/srsepc/src/main.cc b/srsepc/src/main.cc index e8532d1c9..966ff4d21 100644 --- a/srsepc/src/main.cc +++ b/srsepc/src/main.cc @@ -89,8 +89,8 @@ void parse_args(all_args_t* args, int argc, char* argv[]) string mme_apn; string encryption_algo; string integrity_algo; - uint16_t paging_timer; - uint32_t max_paging_queue; + uint16_t paging_timer = 0; + uint32_t max_paging_queue = 0; string spgw_bind_addr; string sgi_if_addr; string sgi_if_name;