From 96b9a33bc16a1d569fe24c3ab1266a7094f41dcb Mon Sep 17 00:00:00 2001 From: Guvenc Gulce Date: Fri, 16 Jul 2021 13:47:39 +0200 Subject: [PATCH 2/3] smc-tools: stats: Fix memory and file handle leaks When exiting the application, make sure that cache file handle is closed and the memory used for the cache file path is freed. Signed-off-by: Guvenc Gulce --- README.md | 1 + stats.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5047f62..175525d 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Release History: Bug fixes: - `smcd`/`smcr`: stats: Fix memory overread in is_data_consistent() + - `smcd`/`smcr`: stats: Fix memory and file handle leaks - `smc_chk`: Remove 'EXPERIMENTAL' flag - `smc_chk`: Improve cleanup - `smc_chk`: Start server with intended port diff --git a/stats.c b/stats.c index d3a814f..4495434 100644 --- a/stats.c +++ b/stats.c @@ -45,7 +45,7 @@ struct smc_stats_rsn smc_rsn; struct smc_stats_rsn smc_rsn_c; struct smc_stats_rsn smc_rsn_org; FILE *cache_fp = NULL; -char *cache_file_path; +char *cache_file_path = NULL; static char* j_output[63] = {"SMC_INT_TX_BUF_8K", "SMC_INT_TX_BUF_16K", "SMC_INT_TX_BUF_32K", "SMC_INT_TX_BUF_64K", "SMC_INT_TX_BUF_128K", "SMC_INT_TX_BUF_256K", "SMC_INT_TX_BUF_512K", "SMC_INT_TX_BUF_1024K", "SMC_INT_TX_BUF_G_1024K", @@ -1011,8 +1011,6 @@ static void fill_cache_file() fprintf(cache_fp, "%16llu\n", smc_rsn.srv_fback_cnt); fprintf(cache_fp, "%16llu\n", smc_rsn.clnt_fback_cnt); - - fclose(cache_fp); } int invoke_stats(int argc, char **argv, int option_details) @@ -1044,10 +1042,18 @@ int invoke_stats(int argc, char **argv, int option_details) print_as_json(); if (reset_cmd) { unlink(cache_file_path); + if (cache_fp) { + fclose(cache_fp); + cache_fp = NULL; + } + free(cache_file_path); + cache_file_path = NULL; open_cache_file(); fill_cache_file(); } errout: + if (cache_fp) + fclose(cache_fp); free(cache_file_path); return 0; } -- 2.25.1