diff --git a/docs/man/xentop.1.pod b/docs/man/xentop.1.pod index db64ceb694..13f3f13246 100644 --- a/docs/man/xentop.1.pod +++ b/docs/man/xentop.1.pod @@ -27,7 +27,7 @@ output version information and exit =item B<-d>, B<--delay>=I -seconds between updates (default 3) +seconds between updates (default 3); must be a non-negative integer =item B<-n>, B<--networks> @@ -55,7 +55,8 @@ output data in batch mode (to stdout) =item B<-i>, B<--iterations>=I -maximum number of iterations xentop should produce before ending +maximum number of iterations xentop should produce before ending; must +be a positive integer =item B<-z>, B<--dom0-first> diff --git a/tools/xentop/xentop.c b/tools/xentop/xentop.c index addb1c70c9..c7fb4ca13f 100644 --- a/tools/xentop/xentop.c +++ b/tools/xentop/xentop.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -1297,6 +1298,28 @@ static void signal_exit_handler(int sig) signal_exit = 1; } +/* Parse a numeric command line argument as a plain decimal integer no + * smaller than min_val. Anything else - a sign, a fractional part, + * trailing junk, overflow - is fatal, rather than being silently + * accepted as a wrong value the way atoi() would. + */ +static unsigned int parse_uint_arg(const char *name, const char *arg, + unsigned int min_val) +{ + unsigned long long val; + char *end; + + errno = 0; + if (isdigit((unsigned char)arg[0])) { + val = strtoull(arg, &end, 10); + if (!errno && !*end && val >= min_val && val <= UINT_MAX) + return val; + } + fprintf(stderr, "xentop: invalid %s argument '%s': expected a %s decimal integer\n", + name, arg, min_val ? "positive" : "non-negative"); + exit(1); +} + int main(int argc, char **argv) { int opt, optind = 0; @@ -1347,7 +1370,7 @@ int main(int argc, char **argv) show_vcpus = 1; break; case 'd': - delay = atoi(optarg); + delay = parse_uint_arg("--delay", optarg, 0); break; case 'b': batch = 1; @@ -1356,7 +1379,7 @@ int main(int argc, char **argv) show_pcpus = 1; break; case 'i': - iterations = atoi(optarg); + iterations = parse_uint_arg("--iterations", optarg, 1); loop = 0; break; case 'f':