diff options
author | Sergey Kandaurov <pluknet@FreeBSD.org> | 2013-08-30 11:21:52 +0000 |
---|---|---|
committer | Sergey Kandaurov <pluknet@FreeBSD.org> | 2013-08-30 11:21:52 +0000 |
commit | 8d99b9cebd80ba6c1e85969fe9d9ea0962d31d14 (patch) | |
tree | ec178df2fdc1c0e4ec69d9cb359f4a92f48929de /lib/libutil/expand_number.c | |
parent | f52924b480873b833669da47bdb8dff79054e67b (diff) | |
download | src-8d99b9cebd80ba6c1e85969fe9d9ea0962d31d14.tar.gz src-8d99b9cebd80ba6c1e85969fe9d9ea0962d31d14.zip |
The round of expand_number() cleanups.
o Fix range error checking to detect overflow when uint64_t < uintmax_t.
o Remove a non-functional check for no valid digits as pointed out by Bruce.
o Remove a rather pointless comment describing what the function does.
o Clean up a bunch of style bugs.
Brucified by: bde
Notes
Notes:
svn path=/head/; revision=255069
Diffstat (limited to 'lib/libutil/expand_number.c')
-rw-r--r-- | lib/libutil/expand_number.c | 39 |
1 files changed, 10 insertions, 29 deletions
diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index 401e2d9ac8b6..24bcc39a35e9 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -35,42 +35,24 @@ __FBSDID("$FreeBSD$"); #include <libutil.h> #include <stdint.h> -/* - * Convert an expression of the following forms to a uint64_t. - * 1) A positive decimal number. - * 2) A positive decimal number followed by a 'b' or 'B' (mult by 1). - * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). - * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). - * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). - * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). - * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). - * 8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60). - */ int expand_number(const char *buf, uint64_t *num) { + char *endptr; + uintmax_t umaxval; uint64_t number; - int saved_errno; unsigned shift; - char *endptr; + int serrno; - saved_errno = errno; + serrno = errno; errno = 0; - - number = strtoumax(buf, &endptr, 0); - - if (number == UINTMAX_MAX && errno == ERANGE) { - return (-1); - } - - if (errno == 0) - errno = saved_errno; - - if (endptr == buf) { - /* No valid digits. */ - errno = EINVAL; + umaxval = strtoumax(buf, &endptr, 0); + if (umaxval > UINT64_MAX) + errno = ERANGE; + if (errno != 0) return (-1); - } + errno = serrno; + number = umaxval; switch (tolower((unsigned char)*endptr)) { case 'e': @@ -106,7 +88,6 @@ expand_number(const char *buf, uint64_t *num) errno = ERANGE; return (-1); } - *num = number << shift; return (0); } |