taskset: provide proper pid validation. When passing the -p option, atoi() does not do a good enough job passing string based number to a proper intenger. Before: dave@cowboy:~/projects/util-linux-ng/schedutils$ ./taskset -p 13crapinput pid 13's current affinity mask: 1 Now: dave@cowboy:~/projects/util-linux-ng/schedutils$ ./taskset -p 13crapinput Usage: taskset [options] [mask | cpu-list] [pid|cmd [args...]] ... Signed-off-by: Davidlohr Bueso <dave@xxxxxxx> --- schedutils/taskset.c | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-) diff --git a/schedutils/taskset.c b/schedutils/taskset.c index 2f1bc74..2ee47d2 100644 --- a/schedutils/taskset.c +++ b/schedutils/taskset.c @@ -63,6 +63,27 @@ static void __attribute__((__noreturn__)) usage(FILE *out) exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } +static long getnum(const char *str) +{ + long num; + char *end = NULL; + + if (!str || *str == '\0') + goto err; + errno = 0; + num = strtol(str, &end, 10); + + if (errno || (end && *end)) + goto err; + + return num; +err: + if (errno) + fprintf(stderr, "cannot parse number '%s'", str); + return 0; +} + + int main(int argc, char *argv[]) { cpu_set_t *new_set, *cur_set; @@ -87,7 +108,7 @@ int main(int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) { switch (opt) { case 'p': - pid = atoi(argv[argc - 1]); + pid = getnum(argv[argc - 1]); break; case 'c': c_opt = 1; -- 1.7.0.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html