Skip to content

Commit

Permalink
cpupower: bench: parse.c: fix several resource leaks
Browse files Browse the repository at this point in the history
The error handling in prepare_output has several issues with
resource leaks.  Ensure that filename is free'd and the directory
stream DIR is closed before returning.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Thomas Renninger <trenn@suse.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  • Loading branch information
Colin Ian King authored and Rafael J. Wysocki committed Apr 28, 2016
1 parent 04b0359 commit 983d9e0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tools/power/cpupower/bench/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ FILE *prepare_output(const char *dirname)

len = strlen(dirname) + 30;
filename = malloc(sizeof(char) * len);
if (!filename) {
perror("malloc");
goto out_dir;
}

if (uname(&sysdata) == 0) {
len += strlen(sysdata.nodename) + strlen(sysdata.release);
filename = realloc(filename, sizeof(char) * len);

if (filename == NULL) {
if (!filename) {
perror("realloc");
return NULL;
goto out_dir;
}

snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
Expand All @@ -104,12 +108,16 @@ FILE *prepare_output(const char *dirname)
if (output == NULL) {
perror("fopen");
fprintf(stderr, "error: unable to open logfile\n");
goto out;
}

fprintf(stdout, "Logfile: %s\n", filename);

free(filename);
fprintf(output, "#round load sleep performance powersave percentage\n");
out:
free(filename);
out_dir:
closedir(dir);
return output;
}

Expand Down

0 comments on commit 983d9e0

Please sign in to comment.