From cdecabb273c2f8b867b0e440f11251cb298ff0e3 Mon Sep 17 00:00:00 2001 From: pjd Date: Wed, 5 Sep 2007 14:27:13 +0000 Subject: - Fix strange for loop. Reported by: phk - While here, check the unit before calculating the actually number. This way we can return EINVAL for invalid unit instead of ERANGE. Approved by: re (kensmith) --- lib/libutil/expand_number.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'lib/libutil/expand_number.c') diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index b9ae5d2..5cc2608 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -68,7 +68,22 @@ expand_number(char *buf, int64_t *num) } s = tolower(*endptr); - for (i = 0; i < unit[i] != '\0'; i++) { + switch (s) { + case 'b': + case 'k': + case 'm': + case 'g': + case 't': + case 'p': + case 'e': + break; + default: + /* Unrecognized unit. */ + errno = EINVAL; + return (-1); + } + + for (i = 0; unit[i] != '\0'; i++) { if (s == unit[i]) break; if ((number < 0 && (number << 10) > number) || @@ -78,11 +93,6 @@ expand_number(char *buf, int64_t *num) } number <<= 10; } - if (unit[i] == '\0') { - /* Unrecognized unit. */ - errno = EINVAL; - return (-1); - } *num = number; return (0); -- cgit v1.1