diff options
author | Mike Frysinger <vapier@chromium.org> | 2016-09-07 20:23:46 -0400 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-09-08 06:14:45 -0700 |
commit | c60eb7e735bbd6d401d7c3c73a7950461cd86d05 (patch) | |
tree | 842c13348c102c5ce711a01e97cd95c3ade36527 /cgpt | |
parent | 61c4ee12be495fe60b94b60f768be0f6a539fd05 (diff) | |
download | vboot-c60eb7e735bbd6d401d7c3c73a7950461cd86d05.tar.gz |
cgpt: unify cli int parsing error checking
Most of the cmd funcs had the same logic copied & pasted multiple times
over. Unify them into a common header.
BUG=chromium:644845
TEST=precq passes
TEST=passing invalid args to some funcs is caught
BRANCH=None
Change-Id: Ib7212bcbb17da1135b2508a52910aac37ee8e6cd
Reviewed-on: https://chromium-review.googlesource.com/382691
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'cgpt')
-rw-r--r-- | cgpt/cgpt.h | 2 | ||||
-rw-r--r-- | cgpt/cgpt_common.c | 16 | ||||
-rw-r--r-- | cgpt/cmd_add.c | 64 | ||||
-rw-r--r-- | cgpt/cmd_boot.c | 12 | ||||
-rw-r--r-- | cgpt/cmd_create.c | 12 | ||||
-rw-r--r-- | cgpt/cmd_find.c | 11 | ||||
-rw-r--r-- | cgpt/cmd_legacy.c | 6 | ||||
-rw-r--r-- | cgpt/cmd_prioritize.c | 23 | ||||
-rw-r--r-- | cgpt/cmd_repair.c | 6 | ||||
-rw-r--r-- | cgpt/cmd_show.c | 12 |
10 files changed, 43 insertions, 121 deletions
diff --git a/cgpt/cgpt.h b/cgpt/cgpt.h index b8b7e8d5..c4edd954 100644 --- a/cgpt/cgpt.h +++ b/cgpt/cgpt.h @@ -187,6 +187,8 @@ void Error(const char *format, ...); void Warning(const char *format, ...); // Command functions. +int check_int_parse(char option, const char *buf); +int check_int_limit(char option, int val, int low, int high); int cmd_show(int argc, char *argv[]); int cmd_repair(int argc, char *argv[]); int cmd_create(int argc, char *argv[]); diff --git a/cgpt/cgpt_common.c b/cgpt/cgpt_common.c index 29fe524a..29906d40 100644 --- a/cgpt/cgpt_common.c +++ b/cgpt/cgpt_common.c @@ -51,6 +51,22 @@ void Warning(const char *format, ...) { va_end(ap); } +int check_int_parse(char option, const char *buf) { + if (!*optarg || (buf && *buf)) { + Error("invalid argument to -%c: \"%s\"\n", option, optarg); + return 1; + } + return 0; +} + +int check_int_limit(char option, int val, int low, int high) { + if (val < low || val > high) { + Error("value for -%c must be between %d and %d", option, low, high); + return 1; + } + return 0; +} + int CheckValid(const struct drive *drive) { if ((drive->gpt.valid_headers != MASK_BOTH) || (drive->gpt.valid_entries != MASK_BOTH)) { diff --git a/cgpt/cmd_add.c b/cgpt/cmd_add.c index 0d89065b..0d5ad0d6 100644 --- a/cgpt/cmd_add.c +++ b/cgpt/cmd_add.c @@ -51,37 +51,21 @@ int cmd_add(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'i': params.partition = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'b': params.set_begin = 1; params.begin = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 's': params.set_size = 1; params.size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 't': params.set_type = 1; @@ -104,51 +88,25 @@ int cmd_add(int argc, char *argv[]) { case 'S': params.set_successful = 1; params.successful = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } - if (params.successful < 0 || params.successful > 1) { - Error("value for -%c must be between 0 and 1", c); - errorcnt++; - } + errorcnt += check_int_parse(c, e); + errorcnt += check_int_limit(c, params.successful, 0, 1); break; case 'T': params.set_tries = 1; params.tries = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n", - progname, c, optarg); - errorcnt++; - } - if (params.tries < 0 || params.tries > 15) { - Error("value for -%c must be between 0 and 15", c); - errorcnt++; - } + errorcnt += check_int_parse(c, e); + errorcnt += check_int_limit(c, params.tries, 0, 15); break; case 'P': params.set_priority = 1; params.priority = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } - if (params.priority < 0 || params.priority > 15) { - Error("value for -%c must be between 0 and 15", c); - errorcnt++; - } + errorcnt += check_int_parse(c, e); + errorcnt += check_int_limit(c, params.priority, 0, 15); break; case 'A': params.set_raw = 1; params.raw_value = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'h': diff --git a/cgpt/cmd_boot.c b/cgpt/cmd_boot.c index 2f815663..a556b53b 100644 --- a/cgpt/cmd_boot.c +++ b/cgpt/cmd_boot.c @@ -43,19 +43,11 @@ int cmd_boot(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'i': params.partition = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'b': params.bootfile = optarg; diff --git a/cgpt/cmd_create.c b/cgpt/cmd_create.c index 34f59cbc..d4ef44c7 100644 --- a/cgpt/cmd_create.c +++ b/cgpt/cmd_create.c @@ -39,22 +39,14 @@ int cmd_create(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'z': params.zap = 1; break; case 'p': params.padding = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'h': Usage(); diff --git a/cgpt/cmd_find.c b/cgpt/cmd_find.c index 68ba6a85..3802902b 100644 --- a/cgpt/cmd_find.c +++ b/cgpt/cmd_find.c @@ -84,11 +84,7 @@ int cmd_find(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'v': params.verbose++; @@ -134,10 +130,7 @@ int cmd_find(int argc, char *argv[]) { break; case 'O': params.matchoffset = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'h': diff --git a/cgpt/cmd_legacy.c b/cgpt/cmd_legacy.c index e45b305c..911384d6 100644 --- a/cgpt/cmd_legacy.c +++ b/cgpt/cmd_legacy.c @@ -38,11 +38,7 @@ int cmd_legacy(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'e': if (params.mode) { diff --git a/cgpt/cmd_prioritize.c b/cgpt/cmd_prioritize.c index ab4e2e36..006f1f12 100644 --- a/cgpt/cmd_prioritize.c +++ b/cgpt/cmd_prioritize.c @@ -52,34 +52,19 @@ int cmd_prioritize(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'i': params.set_partition = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'f': params.set_friends = 1; break; case 'P': params.max_priority = (int)strtol(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } - if (params.max_priority < 1 || params.max_priority > 15) { - Error("value for -%c must be between 1 and 15\n", c); - errorcnt++; - } + errorcnt += check_int_parse(c, e); + errorcnt += check_int_limit(c, params.max_priority, 1, 15); break; case 'h': diff --git a/cgpt/cmd_repair.c b/cgpt/cmd_repair.c index 528c650a..f2534a17 100644 --- a/cgpt/cmd_repair.c +++ b/cgpt/cmd_repair.c @@ -37,11 +37,7 @@ int cmd_repair(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'v': params.verbose++; diff --git a/cgpt/cmd_show.c b/cgpt/cmd_show.c index dcfefdd3..c5a5f445 100644 --- a/cgpt/cmd_show.c +++ b/cgpt/cmd_show.c @@ -52,11 +52,7 @@ int cmd_show(int argc, char *argv[]) { { case 'D': params.drive_size = strtoull(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'n': params.numeric = 1; @@ -69,11 +65,7 @@ int cmd_show(int argc, char *argv[]) { break; case 'i': params.partition = (uint32_t)strtoul(optarg, &e, 0); - if (!*optarg || (e && *e)) - { - Error("invalid argument to -%c: \"%s\"\n", c, optarg); - errorcnt++; - } + errorcnt += check_int_parse(c, e); break; case 'b': case 's': |