From d48eaf61c2fa61cf5ab5dc0ade18ece911ea6e02 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Mon, 18 Jul 2022 19:19:31 +0800 Subject: [PATCH] irqbalance: use endptr to check whether the input parameter is empty Signed-off-by: Liu Chao --- irqbalance.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/irqbalance.c b/irqbalance.c index 5eae5b6..2c9e3f3 100644 --- a/irqbalance.c +++ b/irqbalance.c @@ -124,6 +124,7 @@ static void parse_command_line(int argc, char **argv) int opt; int longind; unsigned long val; + char *endptr; while ((opt = getopt_long(argc, argv, "odfjVi:p:s:c:l:m:t:e:", @@ -139,8 +140,8 @@ static void parse_command_line(int argc, char **argv) exit(1); break; case 'c': - deepest_cache = strtoul(optarg, NULL, 10); - if (deepest_cache == ULONG_MAX || deepest_cache < 1) { + deepest_cache = strtoul(optarg, &endptr, 10); + if (optarg == endptr || deepest_cache == ULONG_MAX || deepest_cache < 1) { usage(); exit(1); } @@ -153,8 +154,8 @@ static void parse_command_line(int argc, char **argv) foreground_mode=1; break; case 'i': - val = strtoull(optarg, NULL, 10); - if (val == ULONG_MAX) { + val = strtoull(optarg, &endptr, 10); + if (optarg == endptr || val == ULONG_MAX) { usage(); exit(1); } @@ -171,8 +172,8 @@ static void parse_command_line(int argc, char **argv) if (!strncmp(optarg, "off", strlen(optarg))) power_thresh = ULONG_MAX; else { - power_thresh = strtoull(optarg, NULL, 10); - if (power_thresh == ULONG_MAX) { + power_thresh = strtoull(optarg, &endptr, 10); + if (optarg == endptr || power_thresh == ULONG_MAX) { usage(); exit(1); } @@ -189,14 +190,18 @@ static void parse_command_line(int argc, char **argv) foreground_mode=1; break; case 't': - sleep_interval = strtol(optarg, NULL, 10); - if (sleep_interval < 1) { + sleep_interval = strtol(optarg, &endptr, 10); + if (optarg == endptr || sleep_interval < 1) { usage(); exit(1); } break; case 'e': - migrate_ratio = strtoul(optarg, NULL, 10); + migrate_ratio = strtoul(optarg, &endptr, 10); + if (optarg == endptr) { + usage(); + exit(1); + } break; } }