diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.c')
| -rw-r--r-- | src/lib/libc/stdlib/strtoul.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c index 00f7210fa1..d3b363fa04 100644 --- a/src/lib/libc/stdlib/strtoul.c +++ b/src/lib/libc/stdlib/strtoul.c | |||
| @@ -32,13 +32,12 @@ | |||
| 32 | */ | 32 | */ |
| 33 | 33 | ||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
| 35 | /*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/ | 35 | static char *rcsid = "$OpenBSD: strtoul.c,v 1.4 1996/08/19 08:33:52 tholo Exp $"; |
| 36 | static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
| 38 | 37 | ||
| 39 | #include <limits.h> | ||
| 40 | #include <ctype.h> | 38 | #include <ctype.h> |
| 41 | #include <errno.h> | 39 | #include <errno.h> |
| 40 | #include <limits.h> | ||
| 42 | #include <stdlib.h> | 41 | #include <stdlib.h> |
| 43 | 42 | ||
| 44 | /* | 43 | /* |
| @@ -53,23 +52,26 @@ strtoul(nptr, endptr, base) | |||
| 53 | char **endptr; | 52 | char **endptr; |
| 54 | register int base; | 53 | register int base; |
| 55 | { | 54 | { |
| 56 | register const char *s = nptr; | 55 | register const char *s; |
| 57 | register unsigned long acc; | 56 | register unsigned long acc, cutoff; |
| 58 | register int c; | 57 | register int c; |
| 59 | register unsigned long cutoff; | 58 | register int neg, any, cutlim; |
| 60 | register int neg = 0, any, cutlim; | ||
| 61 | 59 | ||
| 62 | /* | 60 | /* |
| 63 | * See strtol for comments as to the logic used. | 61 | * See strtol for comments as to the logic used. |
| 64 | */ | 62 | */ |
| 63 | s = nptr; | ||
| 65 | do { | 64 | do { |
| 66 | c = *s++; | 65 | c = (unsigned char) *s++; |
| 67 | } while (isspace(c)); | 66 | } while (isspace(c)); |
| 68 | if (c == '-') { | 67 | if (c == '-') { |
| 69 | neg = 1; | 68 | neg = 1; |
| 70 | c = *s++; | 69 | c = *s++; |
| 71 | } else if (c == '+') | 70 | } else { |
| 72 | c = *s++; | 71 | neg = 0; |
| 72 | if (c == '+') | ||
| 73 | c = *s++; | ||
| 74 | } | ||
| 73 | if ((base == 0 || base == 16) && | 75 | if ((base == 0 || base == 16) && |
| 74 | c == '0' && (*s == 'x' || *s == 'X')) { | 76 | c == '0' && (*s == 'x' || *s == 'X')) { |
| 75 | c = s[1]; | 77 | c = s[1]; |
| @@ -78,9 +80,10 @@ strtoul(nptr, endptr, base) | |||
| 78 | } | 80 | } |
| 79 | if (base == 0) | 81 | if (base == 0) |
| 80 | base = c == '0' ? 8 : 10; | 82 | base = c == '0' ? 8 : 10; |
| 81 | cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; | 83 | |
| 82 | cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; | 84 | cutoff = ULONG_MAX / (unsigned long)base; |
| 83 | for (acc = 0, any = 0;; c = *s++) { | 85 | cutlim = ULONG_MAX % (unsigned long)base; |
| 86 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
| 84 | if (isdigit(c)) | 87 | if (isdigit(c)) |
| 85 | c -= '0'; | 88 | c -= '0'; |
| 86 | else if (isalpha(c)) | 89 | else if (isalpha(c)) |
| @@ -89,18 +92,19 @@ strtoul(nptr, endptr, base) | |||
| 89 | break; | 92 | break; |
| 90 | if (c >= base) | 93 | if (c >= base) |
| 91 | break; | 94 | break; |
| 92 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 95 | if (any < 0) |
| 96 | continue; | ||
| 97 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
| 93 | any = -1; | 98 | any = -1; |
| 94 | else { | 99 | acc = ULONG_MAX; |
| 100 | errno = ERANGE; | ||
| 101 | } else { | ||
| 95 | any = 1; | 102 | any = 1; |
| 96 | acc *= base; | 103 | acc *= (unsigned long)base; |
| 97 | acc += c; | 104 | acc += c; |
| 98 | } | 105 | } |
| 99 | } | 106 | } |
| 100 | if (any < 0) { | 107 | if (neg && any > 0) |
| 101 | acc = ULONG_MAX; | ||
| 102 | errno = ERANGE; | ||
| 103 | } else if (neg) | ||
| 104 | acc = -acc; | 108 | acc = -acc; |
| 105 | if (endptr != 0) | 109 | if (endptr != 0) |
| 106 | *endptr = (char *) (any ? s - 1 : nptr); | 110 | *endptr = (char *) (any ? s - 1 : nptr); |
