diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtol.c')
-rw-r--r-- | src/lib/libc/stdlib/strtol.c | 70 |
1 files changed, 45 insertions, 25 deletions
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c index 6f374abd5f..e4ad557fd5 100644 --- a/src/lib/libc/stdlib/strtol.c +++ b/src/lib/libc/stdlib/strtol.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: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ | 35 | static char *rcsid = "$OpenBSD: strtol.c,v 1.4 1996/08/19 08:33:51 tholo Exp $"; |
36 | static char *rcsid = "$Id: strtol.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 | ||
@@ -54,25 +53,28 @@ strtol(nptr, endptr, base) | |||
54 | char **endptr; | 53 | char **endptr; |
55 | register int base; | 54 | register int base; |
56 | { | 55 | { |
57 | register const char *s = nptr; | 56 | register const char *s; |
58 | register unsigned long acc; | 57 | register long acc, cutoff; |
59 | register int c; | 58 | register int c; |
60 | register unsigned long cutoff; | 59 | register int neg, any, cutlim; |
61 | register int neg = 0, any, cutlim; | ||
62 | 60 | ||
63 | /* | 61 | /* |
64 | * Skip white space and pick up leading +/- sign if any. | 62 | * Skip white space and pick up leading +/- sign if any. |
65 | * If base is 0, allow 0x for hex and 0 for octal, else | 63 | * If base is 0, allow 0x for hex and 0 for octal, else |
66 | * assume decimal; if base is already 16, allow 0x. | 64 | * assume decimal; if base is already 16, allow 0x. |
67 | */ | 65 | */ |
66 | s = nptr; | ||
68 | do { | 67 | do { |
69 | c = *s++; | 68 | c = (unsigned char) *s++; |
70 | } while (isspace(c)); | 69 | } while (isspace(c)); |
71 | if (c == '-') { | 70 | if (c == '-') { |
72 | neg = 1; | 71 | neg = 1; |
73 | c = *s++; | 72 | c = *s++; |
74 | } else if (c == '+') | 73 | } else { |
75 | c = *s++; | 74 | neg = 0; |
75 | if (c == '+') | ||
76 | c = *s++; | ||
77 | } | ||
76 | if ((base == 0 || base == 16) && | 78 | if ((base == 0 || base == 16) && |
77 | c == '0' && (*s == 'x' || *s == 'X')) { | 79 | c == '0' && (*s == 'x' || *s == 'X')) { |
78 | c = s[1]; | 80 | c = s[1]; |
@@ -99,10 +101,17 @@ strtol(nptr, endptr, base) | |||
99 | * Set any if any `digits' consumed; make it negative to indicate | 101 | * Set any if any `digits' consumed; make it negative to indicate |
100 | * overflow. | 102 | * overflow. |
101 | */ | 103 | */ |
102 | cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; | 104 | cutoff = neg ? LONG_MIN : LONG_MAX; |
103 | cutlim = cutoff % (unsigned long)base; | 105 | cutlim = cutoff % base; |
104 | cutoff /= (unsigned long)base; | 106 | cutoff /= base; |
105 | for (acc = 0, any = 0;; c = *s++) { | 107 | if (neg) { |
108 | if (cutlim > 0) { | ||
109 | cutlim -= base; | ||
110 | cutoff += 1; | ||
111 | } | ||
112 | cutlim = -cutlim; | ||
113 | } | ||
114 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
106 | if (isdigit(c)) | 115 | if (isdigit(c)) |
107 | c -= '0'; | 116 | c -= '0'; |
108 | else if (isalpha(c)) | 117 | else if (isalpha(c)) |
@@ -111,19 +120,30 @@ strtol(nptr, endptr, base) | |||
111 | break; | 120 | break; |
112 | if (c >= base) | 121 | if (c >= base) |
113 | break; | 122 | break; |
114 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 123 | if (any < 0) |
115 | any = -1; | 124 | continue; |
116 | else { | 125 | if (neg) { |
117 | any = 1; | 126 | if (acc < cutoff || acc == cutoff && c > cutlim) { |
118 | acc *= base; | 127 | any = -1; |
119 | acc += c; | 128 | acc = LONG_MIN; |
129 | errno = ERANGE; | ||
130 | } else { | ||
131 | any = 1; | ||
132 | acc *= base; | ||
133 | acc -= c; | ||
134 | } | ||
135 | } else { | ||
136 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
137 | any = -1; | ||
138 | acc = LONG_MAX; | ||
139 | errno = ERANGE; | ||
140 | } else { | ||
141 | any = 1; | ||
142 | acc *= base; | ||
143 | acc += c; | ||
144 | } | ||
120 | } | 145 | } |
121 | } | 146 | } |
122 | if (any < 0) { | ||
123 | acc = neg ? LONG_MIN : LONG_MAX; | ||
124 | errno = ERANGE; | ||
125 | } else if (neg) | ||
126 | acc = -acc; | ||
127 | if (endptr != 0) | 147 | if (endptr != 0) |
128 | *endptr = (char *) (any ? s - 1 : nptr); | 148 | *endptr = (char *) (any ? s - 1 : nptr); |
129 | return (acc); | 149 | return (acc); |