diff options
author | pjd <pjd@FreeBSD.org> | 2007-09-05 14:27:13 +0000 |
---|---|---|
committer | pjd <pjd@FreeBSD.org> | 2007-09-05 14:27:13 +0000 |
commit | cdecabb273c2f8b867b0e440f11251cb298ff0e3 (patch) | |
tree | 433fdc111f0371a66bdd38ab663c47034125d48d /lib/libutil/expand_number.c | |
parent | 7002fe2c1fab8be248223d1bb48999e930ee9abb (diff) | |
download | FreeBSD-src-cdecabb273c2f8b867b0e440f11251cb298ff0e3.zip FreeBSD-src-cdecabb273c2f8b867b0e440f11251cb298ff0e3.tar.gz |
- 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)
Diffstat (limited to 'lib/libutil/expand_number.c')
-rw-r--r-- | lib/libutil/expand_number.c | 22 |
1 files changed, 16 insertions, 6 deletions
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); |