diff options
author | Tom Tromey <tromey@cygnus.com> | 1999-04-08 11:57:28 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 1999-04-08 11:57:28 +0000 |
commit | c86e69b2f952249f97b562c25050613fe0be40c1 (patch) | |
tree | d02ba7850a2e25d68af2379a03ee626da087d1a0 /libjava/java/lang/Integer.java | |
parent | e086449d2a918858b784ccc0032d5a87b3e55d06 (diff) | |
download | gcc-c86e69b2f952249f97b562c25050613fe0be40c1.zip gcc-c86e69b2f952249f97b562c25050613fe0be40c1.tar.gz gcc-c86e69b2f952249f97b562c25050613fe0be40c1.tar.bz2 |
Long.java (parseLong): Corrected overflow detection code.
* java/lang/Long.java (parseLong): Corrected overflow detection
code.
* java/lang/Integer.java (parseInt): Corrected overflow detection
code.
From-SVN: r26295
Diffstat (limited to 'libjava/java/lang/Integer.java')
-rw-r--r-- | libjava/java/lang/Integer.java | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java index 54acc27..b4a4fc2 100644 --- a/libjava/java/lang/Integer.java +++ b/libjava/java/lang/Integer.java @@ -203,12 +203,15 @@ public final class Integer extends Number implements Comparable int val = 0; int digval; + int max = MAX_VALUE / radix; + // We can't directly write `max = (MAX_VALUE + 1) / radix'. + // So instead we fake it. + if (isNeg && MAX_VALUE % radix == radix - 1) + ++max; + for ( ; index < len; index++) { - // The the previous loop iteration left us with a negative - // value (which can only be the most negative value, but we - // don't check that), then having more digits is wrong. - if (val == MIN_VALUE) + if (val < 0 || val > max) throw new NumberFormatException(); if ((digval = Character.digit(str.charAt(index), radix)) < 0) @@ -216,17 +219,9 @@ public final class Integer extends Number implements Comparable // Throw an exception for overflow if result is negative. // However, we special-case the most negative value. - val *= radix; - if (val < 0 || val + digval < 0) - { - if (isNeg && val + digval == MIN_VALUE) - { - // Ok. - } - else - throw new NumberFormatException(); - } - val += digval; + val = val * radix + digval; + if (val < 0 && (! isNeg || val != MIN_VALUE)) + throw new NumberFormatException(); } return isNeg ? -(val) : val; |