From 568b5ec632d91c3bc4fcd9d34a964bd76c2773e0 Mon Sep 17 00:00:00 2001 From: Erwan Velu Date: Mon, 26 Jul 2021 00:01:18 +0200 Subject: [PATCH 1/1] engines/exec: Code cleanup to remove leaks As per the coverty reports, there was some issues in my code : - Some structures were not properly freed before returning. - Some file descriptors were not properly closed - Testing with 'if (!int)' isn't a good way to test if the value is negative Signed-off-by: Erwan Velu --- engines/exec.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/engines/exec.c b/engines/exec.c index 5cfb937c..ab3639c5 100644 --- a/engines/exec.c +++ b/engines/exec.c @@ -118,6 +118,7 @@ char *expand_variables(struct thread_options *o, char *arguments) /* %n is replaced by the name of the running job */ expanded_name = str_replace(expanded_runtime, "%n", o->name); + free(expanded_runtime); return expanded_name; } @@ -151,20 +152,23 @@ static int exec_background(struct thread_options *o, struct exec_options *eo) /* Creating the stderr & stdout output files */ outfd = open(outfilename, O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (!outfd) { + if (outfd < 0) { log_err("fio: cannot open output file %s : %s\n", outfilename, strerror(errno)); free(outfilename); free(errfilename); + free(expanded_arguments); return -1; } errfd = open(errfilename, O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (!errfd) { + if (errfd < 0) { log_err("fio: cannot open output file %s : %s\n", errfilename, strerror(errno)); free(outfilename); free(errfilename); + free(expanded_arguments); + close(outfd); return -1; } } else { @@ -184,6 +188,7 @@ static int exec_background(struct thread_options *o, struct exec_options *eo) free(outfilename); free(errfilename); } + free(expanded_arguments); return 0; } @@ -196,6 +201,7 @@ static int exec_background(struct thread_options *o, struct exec_options *eo) free(outfilename); free(errfilename); } + free(expanded_arguments); return -1; } @@ -220,8 +226,10 @@ static int exec_background(struct thread_options *o, struct exec_options *eo) * at all. */ if (expanded_arguments != NULL) { - if (asprintf(&exec_cmd, "%s %s", eo->program, expanded_arguments) < 0) + if (asprintf(&exec_cmd, "%s %s", eo->program, expanded_arguments) < 0) { + free(expanded_arguments); return -1; + } } else { if (asprintf(&exec_cmd, "%s", eo->program) < 0) return -1; @@ -266,6 +274,11 @@ static int exec_background(struct thread_options *o, struct exec_options *eo) execvp(arguments_array[0], arguments_array); } /* We never reach this place */ + /* Let's free the malloc'ed structures to make static checkers happy */ + if (expanded_arguments) + free(expanded_arguments); + if (arguments_array) + free(arguments_array); return 0; } -- 2.25.1