diff options
| author | Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se> | 2014-07-29 12:12:19 -0400 |
|---|---|---|
| committer | Rafael J. Wysocki <rafael.j.wysocki@intel.com> | 2014-07-29 19:57:13 -0400 |
| commit | 13f6de52b149c030b0d529a3d8d68267ed20f01c (patch) | |
| tree | 1cb090f8c033e0f4fd221f0bd84bec4e65dd3fc2 | |
| parent | 97fa1c5ca680bdee2c650e3aadf2a839b92f3f0e (diff) | |
cpupower: bench: parse.c: Fix several minor errors
Resolved several minor errors in prepare_config() and made some additional improvements.
Earlier, the risk of file stream that was not closed. Misuse of strncpy, and the use of strncmp with strlen that makes it pointless.
I also check that sscanf has been successful, otherwise continue to the next line. And minimized the use of magic numbers.
This was found using a static code analysis program called cppcheck.
Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Signed-off-by: Thomas Renninger <trenn@suse.de>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
| -rw-r--r-- | tools/power/cpupower/bench/parse.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c index 543bba14ae2c..f503fb53824e 100644 --- a/tools/power/cpupower/bench/parse.c +++ b/tools/power/cpupower/bench/parse.c | |||
| @@ -158,14 +158,15 @@ struct config *prepare_default_config() | |||
| 158 | int prepare_config(const char *path, struct config *config) | 158 | int prepare_config(const char *path, struct config *config) |
| 159 | { | 159 | { |
| 160 | size_t len = 0; | 160 | size_t len = 0; |
| 161 | char *opt, *val, *line = NULL; | 161 | char opt[16], val[32], *line = NULL; |
| 162 | FILE *configfile = fopen(path, "r"); | 162 | FILE *configfile; |
| 163 | 163 | ||
| 164 | if (config == NULL) { | 164 | if (config == NULL) { |
| 165 | fprintf(stderr, "error: config is NULL\n"); | 165 | fprintf(stderr, "error: config is NULL\n"); |
| 166 | return 1; | 166 | return 1; |
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | configfile = fopen(path, "r"); | ||
| 169 | if (configfile == NULL) { | 170 | if (configfile == NULL) { |
| 170 | perror("fopen"); | 171 | perror("fopen"); |
| 171 | fprintf(stderr, "error: unable to read configfile\n"); | 172 | fprintf(stderr, "error: unable to read configfile\n"); |
| @@ -174,52 +175,54 @@ int prepare_config(const char *path, struct config *config) | |||
| 174 | } | 175 | } |
| 175 | 176 | ||
| 176 | while (getline(&line, &len, configfile) != -1) { | 177 | while (getline(&line, &len, configfile) != -1) { |
| 177 | if (line[0] == '#' || line[0] == ' ') | 178 | if (line[0] == '#' || line[0] == ' ' || line[0] == '\n') |
| 178 | continue; | 179 | continue; |
| 179 | 180 | ||
| 180 | sscanf(line, "%as = %as", &opt, &val); | 181 | if (sscanf(line, "%14s = %30s", opt, val) < 2) |
| 182 | continue; | ||
| 181 | 183 | ||
| 182 | dprintf("parsing: %s -> %s\n", opt, val); | 184 | dprintf("parsing: %s -> %s\n", opt, val); |
| 183 | 185 | ||
| 184 | if (strncmp("sleep", opt, strlen(opt)) == 0) | 186 | if (strcmp("sleep", opt) == 0) |
| 185 | sscanf(val, "%li", &config->sleep); | 187 | sscanf(val, "%li", &config->sleep); |
| 186 | 188 | ||
| 187 | else if (strncmp("load", opt, strlen(opt)) == 0) | 189 | else if (strcmp("load", opt) == 0) |
| 188 | sscanf(val, "%li", &config->load); | 190 | sscanf(val, "%li", &config->load); |
| 189 | 191 | ||
| 190 | else if (strncmp("load_step", opt, strlen(opt)) == 0) | 192 | else if (strcmp("load_step", opt) == 0) |
| 191 | sscanf(val, "%li", &config->load_step); | 193 | sscanf(val, "%li", &config->load_step); |
| 192 | 194 | ||
| 193 | else if (strncmp("sleep_step", opt, strlen(opt)) == 0) | 195 | else if (strcmp("sleep_step", opt) == 0) |
| 194 | sscanf(val, "%li", &config->sleep_step); | 196 | sscanf(val, "%li", &config->sleep_step); |
| 195 | 197 | ||
| 196 | else if (strncmp("cycles", opt, strlen(opt)) == 0) | 198 | else if (strcmp("cycles", opt) == 0) |
| 197 | sscanf(val, "%u", &config->cycles); | 199 | sscanf(val, "%u", &config->cycles); |
| 198 | 200 | ||
| 199 | else if (strncmp("rounds", opt, strlen(opt)) == 0) | 201 | else if (strcmp("rounds", opt) == 0) |
| 200 | sscanf(val, "%u", &config->rounds); | 202 | sscanf(val, "%u", &config->rounds); |
| 201 | 203 | ||
| 202 | else if (strncmp("verbose", opt, strlen(opt)) == 0) | 204 | else if (strcmp("verbose", opt) == 0) |
| 203 | sscanf(val, "%u", &config->verbose); | 205 | sscanf(val, "%u", &config->verbose); |
| 204 | 206 | ||
| 205 | else if (strncmp("output", opt, strlen(opt)) == 0) | 207 | else if (strcmp("output", opt) == 0) |
| 206 | config->output = prepare_output(val); | 208 | config->output = prepare_output(val); |
| 207 | 209 | ||
| 208 | else if (strncmp("cpu", opt, strlen(opt)) == 0) | 210 | else if (strcmp("cpu", opt) == 0) |
| 209 | sscanf(val, "%u", &config->cpu); | 211 | sscanf(val, "%u", &config->cpu); |
| 210 | 212 | ||
| 211 | else if (strncmp("governor", opt, 14) == 0) | 213 | else if (strcmp("governor", opt) == 0) { |
| 212 | strncpy(config->governor, val, 14); | 214 | strncpy(config->governor, val, |
| 215 | sizeof(config->governor)); | ||
| 216 | config->governor[sizeof(config->governor) - 1] = '\0'; | ||
| 217 | } | ||
| 213 | 218 | ||
| 214 | else if (strncmp("priority", opt, strlen(opt)) == 0) { | 219 | else if (strcmp("priority", opt) == 0) { |
| 215 | if (string_to_prio(val) != SCHED_ERR) | 220 | if (string_to_prio(val) != SCHED_ERR) |
| 216 | config->prio = string_to_prio(val); | 221 | config->prio = string_to_prio(val); |
| 217 | } | 222 | } |
| 218 | } | 223 | } |
| 219 | 224 | ||
| 220 | free(line); | 225 | free(line); |
| 221 | free(opt); | ||
| 222 | free(val); | ||
| 223 | 226 | ||
| 224 | return 0; | 227 | return 0; |
| 225 | } | 228 | } |
