summaryrefslogtreecommitdiff
path: root/libjava/java
diff options
context:
space:
mode:
authortromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>1999-04-08 11:57:28 +0000
committertromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>1999-04-08 11:57:28 +0000
commit26d1defa60a70f885db8606107db883f1676ddf3 (patch)
treed02ba7850a2e25d68af2379a03ee626da087d1a0 /libjava/java
parent3102f1df0caddaad91c769efc8b74388f2afd69f (diff)
downloadgcc-26d1defa60a70f885db8606107db883f1676ddf3.tar.gz
* java/lang/Long.java (parseLong): Corrected overflow detection
code. * java/lang/Integer.java (parseInt): Corrected overflow detection code. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@26295 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/java')
-rw-r--r--libjava/java/lang/Integer.java25
-rw-r--r--libjava/java/lang/Long.java25
2 files changed, 20 insertions, 30 deletions
diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java
index 54acc27ed6b..b4a4fc2a2ce 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;
diff --git a/libjava/java/lang/Long.java b/libjava/java/lang/Long.java
index 1ee8bf36277..f79ee7b78b2 100644
--- a/libjava/java/lang/Long.java
+++ b/libjava/java/lang/Long.java
@@ -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;