diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2012-07-09 12:45:31 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2012-07-09 12:45:31 +0000 |
commit | 399b67b3deb0a3eebe4e84f40205c14840555987 (patch) | |
tree | dad653bf869c5d12a2b482153ab72db517239e28 /sbin/fdisk/misc.c | |
parent | e1500ec08c8cc5149f862d71a1a672e0b0dbf54f (diff) |
Use strtonum() instead of strtol() inside ask_num(). Many overflows
are thus avoided. Since bounds are now reliable don't check the
returned value for being in-bounds. Since default value is
forced inside bounds, don't bother being tricky and passing a
default that is outside the bounds being specified.
ok beck@
Diffstat (limited to 'sbin/fdisk/misc.c')
-rw-r--r-- | sbin/fdisk/misc.c | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index ec9b69e4a42..6a02eefd7d5 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.25 2012/07/08 18:29:28 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.26 2012/07/09 12:45:30 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -92,6 +92,7 @@ int ask_num(const char *str, int dflt, int low, int high) { char lbuf[100], *cp; + const char *errstr; size_t lbuflen; int num; @@ -105,24 +106,20 @@ ask_num(const char *str, int dflt, int low, int high) if (fgets(lbuf, sizeof lbuf, stdin) == NULL) errx(1, "eof"); + lbuflen = strlen(lbuf); if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n') lbuf[lbuflen - 1] = '\0'; - /* Convert */ - cp = lbuf; - num = strtol(lbuf, &cp, 10); - - /* Make sure only number present */ - if (cp == lbuf) + if (lbuf[0] == '\0') { num = dflt; - if (*cp != '\0') { - printf("'%s' is not a valid number.\n", lbuf); - num = low - 1; - } else if (num < low || num > high) { - printf("'%d' is out of range.\n", num); + errstr = NULL; + } else { + num = (int)strtonum(lbuf, low, high, &errstr); + if (errstr) + printf("%s is %s: %s.\n", str, errstr, lbuf); } - } while (num < low || num > high); + } while (errstr); return (num); } |