summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkrw <krw@openbsd.org>2012-07-09 12:45:30 +0000
committerkrw <krw@openbsd.org>2012-07-09 12:45:30 +0000
commit0cdbad8ad828a84903768c694d3876fa57581c53 (patch)
treea3aab5394daee885cad0812c51176791648a61f3
parentmove to the new resolver implementation, with temporary glue to use (diff)
downloadwireguard-openbsd-0cdbad8ad828a84903768c694d3876fa57581c53.tar.xz
wireguard-openbsd-0cdbad8ad828a84903768c694d3876fa57581c53.zip
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@
-rw-r--r--sbin/fdisk/cmd.c8
-rw-r--r--sbin/fdisk/misc.c23
2 files changed, 12 insertions, 19 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c
index ecf02a98326..c6155da1c9f 100644
--- a/sbin/fdisk/cmd.c
+++ b/sbin/fdisk/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.53 2012/07/08 18:29:28 krw Exp $ */
+/* $OpenBSD: cmd.c,v 1.54 2012/07/09 12:45:30 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -112,11 +112,7 @@ Xswap(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset)
return (ret);
}
- pt = ask_num("Swap with what partition?", -1, 0, 3);
- if (pt < 0 || pt > 3) {
- printf("Invalid partition number %d.\n", pt);
- return (ret);
- }
+ pt = ask_num("Swap with what partition?", 0, 0, 3);
if (pt == pf) {
printf("%d same partition as %d, doing nothing.\n", pt, pf);
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);
}