From b5645545274b51f776daadcb1b7d2623708522b3 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 15 Jun 1999 12:08:14 +0000 Subject: [PATCH] It is not generally true that if (unsigned)a*(unsigned)b overflows, then the result is less than 'a'. --- stdlib/strtol.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/stdlib/strtol.c b/stdlib/strtol.c index 6ba2960f295..42da792c44c 100644 --- a/stdlib/strtol.c +++ b/stdlib/strtol.c @@ -348,6 +348,7 @@ INTERNAL (strtol) (nptr, endptr, base, group LOCALE_PARAM) if (sizeof (long int) != sizeof (LONG int)) { unsigned long int j = 0; + unsigned long int jmax = ULONG_MAX / base; for (;c != L_('\0'); c = *++s) { @@ -362,18 +363,14 @@ INTERNAL (strtol) (nptr, endptr, base, group LOCALE_PARAM) if ((int) c >= base) break; /* Note that we never can have an overflow. */ - else + else if (j >= jmax) { - unsigned long int jj = j * (unsigned long int) base; - if (jj < j) - { - /* We have an overflow. Now use the long representation. */ - i = (unsigned LONG int) j; - goto use_long; - } - j = jj; - j += c; + /* We have an overflow. Now use the long representation. */ + i = (unsigned LONG int) j; + goto use_long; } + else + j = j * (unsigned long int) base + c; } i = (unsigned LONG int) j; -- 2.47.3