]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Long.java (parseLong): Corrected overflow detection code.
authorTom Tromey <tromey@cygnus.com>
Thu, 8 Apr 1999 11:57:28 +0000 (11:57 +0000)
committerTom Tromey <tromey@gcc.gnu.org>
Thu, 8 Apr 1999 11:57:28 +0000 (11:57 +0000)
* java/lang/Long.java (parseLong): Corrected overflow detection
code.
* java/lang/Integer.java (parseInt): Corrected overflow detection
code.

From-SVN: r26295

libjava/ChangeLog
libjava/java/lang/Integer.java
libjava/java/lang/Long.java

index eaeff0b0edb395ae3798885d35980cd995ba5942..c97368a77352ba3cd2bc30d8e5060905d698a04b 100644 (file)
@@ -1,5 +1,10 @@
 1999-04-08  Tom Tromey  <tromey@cygnus.com>
 
+       * java/lang/Long.java (parseLong): Corrected overflow detection
+       code.
+       * java/lang/Integer.java (parseInt): Corrected overflow detection
+       code.
+
        * java/io/PrintStream.java (print): Handle null string argument.
        (println): Likewise.
 
index 54acc27ed6b20f5dc8229bebf8d3209cfe38b9cf..b4a4fc2a2ceb5b3af4d0a60474299912c0d5e04b 100644 (file)
@@ -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;
index 1ee8bf362779efcd77d56a799fa445fc2b736710..f79ee7b78b2f13c21e2128a9e25881ce0b5ae0ca 100644 (file)
@@ -205,12 +205,15 @@ public final class Long extends Number implements Comparable
     long val = 0;
     int digval;
 
+    long 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)
@@ -218,17 +221,9 @@ public final class Long 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;