Commit c86e69b2 by Tom Tromey Committed by Tom Tromey

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
parent e086449d
1999-04-08 Tom Tromey <tromey@cygnus.com> 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. * java/io/PrintStream.java (print): Handle null string argument.
(println): Likewise. (println): Likewise.
......
...@@ -203,12 +203,15 @@ public final class Integer extends Number implements Comparable ...@@ -203,12 +203,15 @@ public final class Integer extends Number implements Comparable
int val = 0; int val = 0;
int digval; 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++) for ( ; index < len; index++)
{ {
// The the previous loop iteration left us with a negative if (val < 0 || val > max)
// 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)
throw new NumberFormatException(); throw new NumberFormatException();
if ((digval = Character.digit(str.charAt(index), radix)) < 0) if ((digval = Character.digit(str.charAt(index), radix)) < 0)
...@@ -216,18 +219,10 @@ public final class Integer extends Number implements Comparable ...@@ -216,18 +219,10 @@ public final class Integer extends Number implements Comparable
// Throw an exception for overflow if result is negative. // Throw an exception for overflow if result is negative.
// However, we special-case the most negative value. // However, we special-case the most negative value.
val *= radix; val = val * radix + digval;
if (val < 0 || val + digval < 0) if (val < 0 && (! isNeg || val != MIN_VALUE))
{
if (isNeg && val + digval == MIN_VALUE)
{
// Ok.
}
else
throw new NumberFormatException(); throw new NumberFormatException();
} }
val += digval;
}
return isNeg ? -(val) : val; return isNeg ? -(val) : val;
} }
......
...@@ -205,12 +205,15 @@ public final class Long extends Number implements Comparable ...@@ -205,12 +205,15 @@ public final class Long extends Number implements Comparable
long val = 0; long val = 0;
int digval; 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++) for ( ; index < len; index++)
{ {
// The the previous loop iteration left us with a negative if (val < 0 || val > max)
// 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)
throw new NumberFormatException(); throw new NumberFormatException();
if ((digval = Character.digit(str.charAt(index), radix)) < 0) if ((digval = Character.digit(str.charAt(index), radix)) < 0)
...@@ -218,18 +221,10 @@ public final class Long extends Number implements Comparable ...@@ -218,18 +221,10 @@ public final class Long extends Number implements Comparable
// Throw an exception for overflow if result is negative. // Throw an exception for overflow if result is negative.
// However, we special-case the most negative value. // However, we special-case the most negative value.
val *= radix; val = val * radix + digval;
if (val < 0 || val + digval < 0) if (val < 0 && (! isNeg || val != MIN_VALUE))
{
if (isNeg && val + digval == MIN_VALUE)
{
// Ok.
}
else
throw new NumberFormatException(); throw new NumberFormatException();
} }
val += digval;
}
return isNeg ? -(val) : val; return isNeg ? -(val) : val;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment