From: Erwan Velu Date: Sun, 25 Jul 2021 22:01:18 +0000 (+0200) Subject: engines/exec: Code cleanup to remove leaks X-Git-Tag: fio-3.28~39^2 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=568b5ec632d91c3bc4fcd9d34a964bd76c2773e0;hp=65739ddf844203f4c64037e331bf67b63c2a7153;ds=inline 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 --- 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; }