diff options
author | Colin Ian King <colin.king@canonical.com> | 2016-04-28 09:24:37 -0400 |
---|---|---|
committer | Rafael J. Wysocki <rafael.j.wysocki@intel.com> | 2016-04-28 10:02:28 -0400 |
commit | 983d9e065b37dac5aaa2d5a819bdd5b5a0b78c8c (patch) | |
tree | 77545e6fe46603334d6c6585ae00532f024e6872 | |
parent | 04b03594c73c1877a98c7a657cc45bf5e9d89f7c (diff) |
cpupower: bench: parse.c: fix several resource leaks
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>
-rw-r--r-- | tools/power/cpupower/bench/parse.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c index f503fb53824e..2d09c9221ff1 100644 --- a/tools/power/cpupower/bench/parse.c +++ b/tools/power/cpupower/bench/parse.c | |||
@@ -81,14 +81,18 @@ FILE *prepare_output(const char *dirname) | |||
81 | 81 | ||
82 | len = strlen(dirname) + 30; | 82 | len = strlen(dirname) + 30; |
83 | filename = malloc(sizeof(char) * len); | 83 | filename = malloc(sizeof(char) * len); |
84 | if (!filename) { | ||
85 | perror("malloc"); | ||
86 | goto out_dir; | ||
87 | } | ||
84 | 88 | ||
85 | if (uname(&sysdata) == 0) { | 89 | if (uname(&sysdata) == 0) { |
86 | len += strlen(sysdata.nodename) + strlen(sysdata.release); | 90 | len += strlen(sysdata.nodename) + strlen(sysdata.release); |
87 | filename = realloc(filename, sizeof(char) * len); | 91 | filename = realloc(filename, sizeof(char) * len); |
88 | 92 | ||
89 | if (filename == NULL) { | 93 | if (!filename) { |
90 | perror("realloc"); | 94 | perror("realloc"); |
91 | return NULL; | 95 | goto out_dir; |
92 | } | 96 | } |
93 | 97 | ||
94 | snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log", | 98 | snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log", |
@@ -104,12 +108,16 @@ FILE *prepare_output(const char *dirname) | |||
104 | if (output == NULL) { | 108 | if (output == NULL) { |
105 | perror("fopen"); | 109 | perror("fopen"); |
106 | fprintf(stderr, "error: unable to open logfile\n"); | 110 | fprintf(stderr, "error: unable to open logfile\n"); |
111 | goto out; | ||
107 | } | 112 | } |
108 | 113 | ||
109 | fprintf(stdout, "Logfile: %s\n", filename); | 114 | fprintf(stdout, "Logfile: %s\n", filename); |
110 | 115 | ||
111 | free(filename); | ||
112 | fprintf(output, "#round load sleep performance powersave percentage\n"); | 116 | fprintf(output, "#round load sleep performance powersave percentage\n"); |
117 | out: | ||
118 | free(filename); | ||
119 | out_dir: | ||
120 | closedir(dir); | ||
113 | return output; | 121 | return output; |
114 | } | 122 | } |
115 | 123 | ||