diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2007-03-18 16:16:57 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2007-03-18 16:16:57 +0000 |
commit | cce4628a20a0359b28448856f1ed8515cd73a30d (patch) | |
tree | 7d35c750e91fafdc11f1f19335b98d9c03ec4355 | |
parent | 4e690646a79464178bbb3367c068ce4230d0303d (diff) |
use strtonum() to avoid out of range values, way simpler than the
12-line song and dance that standard functions need; bret.lambert@gmail
-rw-r--r-- | usr.sbin/gpioctl/gpioctl.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/usr.sbin/gpioctl/gpioctl.c b/usr.sbin/gpioctl/gpioctl.c index 50e9ea09cf3..0825af6d66e 100644 --- a/usr.sbin/gpioctl/gpioctl.c +++ b/usr.sbin/gpioctl/gpioctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpioctl.c,v 1.4 2005/11/17 10:18:18 grange Exp $ */ +/* $OpenBSD: gpioctl.c,v 1.5 2007/03/18 16:16:56 deraadt Exp $ */ /* * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> * @@ -22,6 +22,7 @@ #include <sys/types.h> #include <sys/gpio.h> #include <sys/ioctl.h> +#include <sys/limits.h> #include <err.h> #include <fcntl.h> @@ -61,7 +62,7 @@ int main(int argc, char *argv[]) { int ch; - char *ep; + const char *errstr; int do_ctl = 0; int pin = 0, value = 0; @@ -84,8 +85,8 @@ main(int argc, char *argv[]) argv += optind; if (argc > 0) { - pin = strtol(argv[0], &ep, 10); - if (*argv[0] == '\0' || *ep != '\0' || pin < 0) + pin = strtonum(argv[0], 0, INT_MAX, &errstr); + if (errstr) errx(1, "%s: invalid pin", argv[0]); } @@ -103,8 +104,8 @@ main(int argc, char *argv[]) if (do_ctl) { pinctl(pin, argv + 1, argc - 1); } else { - value = strtol(argv[1], &ep, 10); - if (*argv[1] == '\0' || *ep != '\0') + value = strtonum(argv[1], INT_MIN, INT_MAX, &errstr); + if (errstr) errx(1, "%s: invalid value", argv[1]); pinwrite(pin, value); } |