Browse Source

from netbsd; Rearrange to avoid sign problems with GCC.

OPENBSD_2_0
deraadt 29 years ago
parent
commit
2214b38cd7
4 changed files with 115 additions and 73 deletions
  1. +43
    -22
      src/lib/libc/stdlib/strtol.c
  2. +35
    -19
      src/lib/libc/stdlib/strtoq.c
  3. +22
    -17
      src/lib/libc/stdlib/strtoul.c
  4. +15
    -15
      src/lib/libc/stdlib/strtouq.c

+ 43
- 22
src/lib/libc/stdlib/strtol.c View File

@ -33,12 +33,12 @@
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ /*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/
static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
static char *rcsid = "$Id: strtol.c,v 1.2 1995/12/21 14:58:36 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <limits.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
@ -54,25 +54,28 @@ strtol(nptr, endptr, base)
char **endptr; char **endptr;
register int base; register int base;
{ {
register const char *s = nptr;
register unsigned long acc;
register const char *s;
register long acc, cutoff;
register int c; register int c;
register unsigned long cutoff;
register int neg = 0, any, cutlim;
register int neg, any, cutlim;
/* /*
* Skip white space and pick up leading +/- sign if any. * Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else * If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x. * assume decimal; if base is already 16, allow 0x.
*/ */
s = nptr;
do { do {
c = *s++; c = *s++;
} while (isspace(c)); } while (isspace(c));
if (c == '-') { if (c == '-') {
neg = 1; neg = 1;
c = *s++; c = *s++;
} else if (c == '+')
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) && if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) { c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1]; c = s[1];
@ -99,9 +102,16 @@ strtol(nptr, endptr, base)
* Set any if any `digits' consumed; make it negative to indicate * Set any if any `digits' consumed; make it negative to indicate
* overflow. * overflow.
*/ */
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
cutlim = cutoff % (unsigned long)base;
cutoff /= (unsigned long)base;
cutoff = neg ? LONG_MIN : LONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
if (neg) {
if (cutlim > 0) {
cutlim -= base;
cutoff += 1;
}
cutlim = -cutlim;
}
for (acc = 0, any = 0;; c = *s++) { for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c)) if (isdigit(c))
c -= '0'; c -= '0';
@ -111,19 +121,30 @@ strtol(nptr, endptr, base)
break; break;
if (c >= base) if (c >= base)
break; break;
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
any = -1;
else {
any = 1;
acc *= base;
acc += c;
if (any < 0)
continue;
if (neg) {
if (acc < cutoff || acc == cutoff && c > cutlim) {
any = -1;
acc = LONG_MIN;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc -= c;
}
} else {
if (acc > cutoff || acc == cutoff && c > cutlim) {
any = -1;
acc = LONG_MAX;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc += c;
}
} }
} }
if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0) if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);
return (acc); return (acc);


+ 35
- 19
src/lib/libc/stdlib/strtoq.c View File

@ -37,9 +37,9 @@ static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92";
#include <sys/types.h> #include <sys/types.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -55,9 +55,8 @@ strtoq(nptr, endptr, base)
register int base; register int base;
{ {
register const char *s; register const char *s;
register u_quad_t acc;
register quad_t acc, cutoff;
register int c; register int c;
register u_quad_t qbase, cutoff;
register int neg, any, cutlim; register int neg, any, cutlim;
/* /*
@ -104,10 +103,16 @@ strtoq(nptr, endptr, base)
* Set any if any `digits' consumed; make it negative to indicate * Set any if any `digits' consumed; make it negative to indicate
* overflow. * overflow.
*/ */
qbase = (unsigned)base;
cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
cutlim = cutoff % qbase;
cutoff /= qbase;
cutoff = neg ? QUAD_MIN : QUAD_MAX;
cutlim = cutoff % base;
cutoff /= base;
if (neg) {
if (cutlim > 0) {
cutlim -= base;
cutoff += 1;
}
cutlim = -cutlim;
}
for (acc = 0, any = 0;; c = *s++) { for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c)) if (isdigit(c))
c -= '0'; c -= '0';
@ -117,19 +122,30 @@ strtoq(nptr, endptr, base)
break; break;
if (c >= base) if (c >= base)
break; break;
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
any = -1;
else {
any = 1;
acc *= qbase;
acc += c;
if (any < 0)
continue;
if (neg) {
if (acc < cutoff || acc == cutoff && c > cutlim) {
any = -1;
acc = QUAD_MIN;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc -= c;
}
} else {
if (acc > cutoff || acc == cutoff && c > cutlim) {
any = -1;
acc = QUAD_MAX;
errno = ERANGE;
} else {
any = 1;
acc *= base;
acc += c;
}
} }
} }
if (any < 0) {
acc = neg ? QUAD_MIN : QUAD_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0) if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);
return (acc); return (acc);


+ 22
- 17
src/lib/libc/stdlib/strtoul.c View File

@ -33,12 +33,12 @@
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/ /*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/
static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
static char *rcsid = "$Id: strtoul.c,v 1.2 1995/12/21 14:58:38 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <limits.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -53,23 +53,26 @@ strtoul(nptr, endptr, base)
char **endptr; char **endptr;
register int base; register int base;
{ {
register const char *s = nptr;
register unsigned long acc;
register const char *s;
register unsigned long acc, cutoff;
register int c; register int c;
register unsigned long cutoff;
register int neg = 0, any, cutlim;
register int neg, any, cutlim;
/* /*
* See strtol for comments as to the logic used. * See strtol for comments as to the logic used.
*/ */
s = nptr;
do { do {
c = *s++; c = *s++;
} while (isspace(c)); } while (isspace(c));
if (c == '-') { if (c == '-') {
neg = 1; neg = 1;
c = *s++; c = *s++;
} else if (c == '+')
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) && if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) { c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1]; c = s[1];
@ -78,8 +81,9 @@ strtoul(nptr, endptr, base)
} }
if (base == 0) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
cutoff = ULONG_MAX / (unsigned long)base;
cutlim = ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) { for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c)) if (isdigit(c))
c -= '0'; c -= '0';
@ -89,18 +93,19 @@ strtoul(nptr, endptr, base)
break; break;
if (c >= base) if (c >= base)
break; break;
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
if (any < 0)
continue;
if (acc > cutoff || acc == cutoff && c > cutlim) {
any = -1; any = -1;
else {
acc = ULONG_MAX;
errno = ERANGE;
} else {
any = 1; any = 1;
acc *= base;
acc *= (unsigned long)base;
acc += c; acc += c;
} }
} }
if (any < 0) {
acc = ULONG_MAX;
errno = ERANGE;
} else if (neg)
if (neg && any > 0)
acc = -acc; acc = -acc;
if (endptr != 0) if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);


+ 15
- 15
src/lib/libc/stdlib/strtouq.c View File

@ -37,9 +37,9 @@ static char sccsid[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92";
#include <sys/types.h> #include <sys/types.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -54,10 +54,9 @@ strtouq(nptr, endptr, base)
char **endptr; char **endptr;
register int base; register int base;
{ {
register const char *s = nptr;
register u_quad_t acc;
register const char *s;
register u_quad_t acc, cutoff;
register int c; register int c;
register u_quad_t qbase, cutoff;
register int neg, any, cutlim; register int neg, any, cutlim;
/* /*
@ -83,9 +82,9 @@ strtouq(nptr, endptr, base)
} }
if (base == 0) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
qbase = (unsigned)base;
cutoff = (u_quad_t)UQUAD_MAX / qbase;
cutlim = (u_quad_t)UQUAD_MAX % qbase;
cutoff = UQUAD_MAX / (u_quad_t)base;
cutlim = UQUAD_MAX % (u_quad_t)base;
for (acc = 0, any = 0;; c = *s++) { for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c)) if (isdigit(c))
c -= '0'; c -= '0';
@ -95,18 +94,19 @@ strtouq(nptr, endptr, base)
break; break;
if (c >= base) if (c >= base)
break; break;
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
if (any < 0)
continue;
if (acc > cutoff || acc == cutoff && c > cutlim) {
any = -1; any = -1;
else {
acc = UQUAD_MAX;
errno = ERANGE;
} else {
any = 1; any = 1;
acc *= qbase;
acc *= (u_quad_t)base;
acc += c; acc += c;
} }
} }
if (any < 0) {
acc = UQUAD_MAX;
errno = ERANGE;
} else if (neg)
if (neg && any > 0)
acc = -acc; acc = -acc;
if (endptr != 0) if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr); *endptr = (char *) (any ? s - 1 : nptr);


Loading…
Cancel
Save