diff options
author | Erwan Velu <erwanaliasr1@gmail.com> | 2021-07-26 00:01:18 +0200 |
---|---|---|
committer | Erwan Velu <erwanaliasr1@gmail.com> | 2021-07-26 00:01:18 +0200 |
commit | 568b5ec632d91c3bc4fcd9d34a964bd76c2773e0 (patch) | |
tree | b231ea5d5ad8e2e54a7506878be5a77e33ee045a /engines/exec.c | |
parent | 65739ddf844203f4c64037e331bf67b63c2a7153 (diff) | |
download | fio-568b5ec632d91c3bc4fcd9d34a964bd76c2773e0.tar.gz fio-568b5ec632d91c3bc4fcd9d34a964bd76c2773e0.tar.bz2 |
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 <erwanaliasr1@gmail.com>
Diffstat (limited to 'engines/exec.c')
-rw-r--r-- | engines/exec.c | 19 |
1 files 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; } |