From: Thomas Renninger <trenn@suse.de>
To: rjw@rjwysocki.net
Cc: linux-pm@vger.kernel.org,
Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>,
Thomas Renninger <trenn@suse.de>
Subject: [PATCH 2/3] tools: power: cpupower: bench: parse.c: Fix several minor errors
Date: Tue, 29 Jul 2014 18:12:19 +0200 [thread overview]
Message-ID: <1406650340-38644-2-git-send-email-trenn@suse.de> (raw)
In-Reply-To: <1406650340-38644-1-git-send-email-trenn@suse.de>
From: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
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>
---
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 543bba1..f503fb5 100644
--- a/tools/power/cpupower/bench/parse.c
+++ b/tools/power/cpupower/bench/parse.c
@@ -158,14 +158,15 @@ struct config *prepare_default_config()
int prepare_config(const char *path, struct config *config)
{
size_t len = 0;
- char *opt, *val, *line = NULL;
- FILE *configfile = fopen(path, "r");
+ char opt[16], val[32], *line = NULL;
+ FILE *configfile;
if (config == NULL) {
fprintf(stderr, "error: config is NULL\n");
return 1;
}
+ configfile = fopen(path, "r");
if (configfile == NULL) {
perror("fopen");
fprintf(stderr, "error: unable to read configfile\n");
@@ -174,52 +175,54 @@ int prepare_config(const char *path, struct config *config)
}
while (getline(&line, &len, configfile) != -1) {
- if (line[0] == '#' || line[0] == ' ')
+ if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
continue;
- sscanf(line, "%as = %as", &opt, &val);
+ if (sscanf(line, "%14s = %30s", opt, val) < 2)
+ continue;
dprintf("parsing: %s -> %s\n", opt, val);
- if (strncmp("sleep", opt, strlen(opt)) == 0)
+ if (strcmp("sleep", opt) == 0)
sscanf(val, "%li", &config->sleep);
- else if (strncmp("load", opt, strlen(opt)) == 0)
+ else if (strcmp("load", opt) == 0)
sscanf(val, "%li", &config->load);
- else if (strncmp("load_step", opt, strlen(opt)) == 0)
+ else if (strcmp("load_step", opt) == 0)
sscanf(val, "%li", &config->load_step);
- else if (strncmp("sleep_step", opt, strlen(opt)) == 0)
+ else if (strcmp("sleep_step", opt) == 0)
sscanf(val, "%li", &config->sleep_step);
- else if (strncmp("cycles", opt, strlen(opt)) == 0)
+ else if (strcmp("cycles", opt) == 0)
sscanf(val, "%u", &config->cycles);
- else if (strncmp("rounds", opt, strlen(opt)) == 0)
+ else if (strcmp("rounds", opt) == 0)
sscanf(val, "%u", &config->rounds);
- else if (strncmp("verbose", opt, strlen(opt)) == 0)
+ else if (strcmp("verbose", opt) == 0)
sscanf(val, "%u", &config->verbose);
- else if (strncmp("output", opt, strlen(opt)) == 0)
+ else if (strcmp("output", opt) == 0)
config->output = prepare_output(val);
- else if (strncmp("cpu", opt, strlen(opt)) == 0)
+ else if (strcmp("cpu", opt) == 0)
sscanf(val, "%u", &config->cpu);
- else if (strncmp("governor", opt, 14) == 0)
- strncpy(config->governor, val, 14);
+ else if (strcmp("governor", opt) == 0) {
+ strncpy(config->governor, val,
+ sizeof(config->governor));
+ config->governor[sizeof(config->governor) - 1] = '\0';
+ }
- else if (strncmp("priority", opt, strlen(opt)) == 0) {
+ else if (strcmp("priority", opt) == 0) {
if (string_to_prio(val) != SCHED_ERR)
config->prio = string_to_prio(val);
}
}
free(line);
- free(opt);
- free(val);
return 0;
}
--
1.7.6.1
next prev parent reply other threads:[~2014-07-29 16:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-22 14:05 [PATCH 0/4] tools/power/cpupower latest fixes Thomas Renninger
2014-07-22 14:05 ` [PATCH 1/4] cpupower: mperf monitor: Correct use of ! and & Thomas Renninger
2014-07-22 14:05 ` [PATCH 2/4] tools: power: cpupower: bench: parse.c: Fix several minor errors Thomas Renninger
2014-07-22 14:06 ` [PATCH 3/4] cpupower: Remove redundant error check Thomas Renninger
2014-07-22 14:06 ` [PATCH 4/4] cpupower: Adjust MAINTAINERS file Thomas Renninger
2014-07-22 23:53 ` [PATCH 0/4] tools/power/cpupower latest fixes Rafael J. Wysocki
2014-07-29 16:12 ` [PATCH 1/3] cpupower: mperf monitor: Correct use of ! and & Thomas Renninger
2014-07-29 16:12 ` Thomas Renninger [this message]
2014-07-29 16:12 ` [PATCH 3/3] cpupower: Remove redundant error check Thomas Renninger
2014-07-30 0:28 ` [PATCH 1/3] cpupower: mperf monitor: Correct use of ! and & Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1406650340-38644-2-git-send-email-trenn@suse.de \
--to=trenn@suse.de \
--cc=linux-pm@vger.kernel.org \
--cc=rickard_strandqvist@spectrumdigital.se \
--cc=rjw@rjwysocki.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.