From 7ce0a91746ccd2f8658f3056a3e8148a627309db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gordan=20Marku=C5=A1?= Date: Tue, 12 Sep 2017 15:18:13 +0200 Subject: fix various memory leaks (#28) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * dlt_common: Fix resource leak Close the file before returning from function. Signed-off-by: Gordan Markuš * dlt-kpi-process: Fix memory leak Signed-off-by: Gordan Markuš * dlt-kpi-options: Clean up resources on malloc fail Signed-off-by: Gordan Markuš --- src/kpi/dlt-kpi-options.c | 23 +++++++++++------------ src/kpi/dlt-kpi-process.c | 3 ++- src/shared/dlt_common.c | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/kpi/dlt-kpi-options.c b/src/kpi/dlt-kpi-options.c index ea15961..0f6938c 100644 --- a/src/kpi/dlt-kpi-options.c +++ b/src/kpi/dlt-kpi-options.c @@ -117,7 +117,10 @@ void dlt_kpi_init_configuration(DltKpiConfig *config) DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_name) { FILE *file; - char *line, *token, *value, *pch, *strchk; + char *line = NULL; + char *token = NULL; + char *value = NULL; + char *pch, *strchk; int tmp; if(config == NULL || file_name == NULL) @@ -136,20 +139,16 @@ DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_ return DLT_RETURN_ERROR; } - if((line = malloc(COMMAND_LINE_SIZE)) == 0) - { - fprintf(stderr, "%s: Out of memory!\n", __func__); - return DLT_RETURN_ERROR; - } + if(((line = malloc(COMMAND_LINE_SIZE)) == 0) || + ((token = malloc(COMMAND_LINE_SIZE)) == 0) || + ((value = malloc(COMMAND_LINE_SIZE)) == 0)) - if((token = malloc(COMMAND_LINE_SIZE)) == 0) { - fprintf(stderr, "%s: Out of memory!\n", __func__); - return DLT_RETURN_ERROR; - } + fclose(file); + free(line); + free(token); + free(value); - if((value = malloc(COMMAND_LINE_SIZE)) == 0) - { fprintf(stderr, "%s: Out of memory!\n", __func__); return DLT_RETURN_ERROR; } diff --git a/src/kpi/dlt-kpi-process.c b/src/kpi/dlt-kpi-process.c index ad157e1..22a46e6 100644 --- a/src/kpi/dlt-kpi-process.c +++ b/src/kpi/dlt-kpi-process.c @@ -256,7 +256,8 @@ DltKpiProcess *dlt_kpi_clone_process(DltKpiProcess *original) if(new_process->command_line == NULL) { fprintf(stderr, "%s: Out of Memory\n", __func__); - return NULL; + free(new_process); + return NULL; } strncpy(new_process->command_line, original->command_line, strlen(original->command_line) + 1); } diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 8808801..b98c7fa 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -477,7 +477,6 @@ DltReturnValue dlt_filter_load(DltFilter *filter, const char *filename, int verb { snprintf(str,DLT_COMMON_BUFFER_LENGTH, "Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n", DLT_FILTER_MAX); dlt_log(LOG_WARNING, str); - return DLT_RETURN_OK; } } -- cgit v1.2.1