Tighten sanity checks on BigDecimal(String value) so that it doesn't accept
strings with 2 signs.
Review at http://gwt-code-reviews.appspot.com/1424806
Review by: jat@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10048 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/math/BigDecimal.java b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
index 0cd351f..3c29c85 100644
--- a/user/super/com/google/gwt/emul/java/math/BigDecimal.java
+++ b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
@@ -2608,6 +2608,12 @@
if ((offset < last) && (val.charAt(offset) == '+')) {
offset++;
begin++;
+
+ // Fail if the next character is another sign.
+ if ((offset < last)
+ && (val.charAt(offset) == '+' || val.charAt(offset) == '-')) {
+ throw new NumberFormatException("For input string: \"" + val + "\"");
+ }
}
int counter = 0;
boolean wasNonZero = false;
diff --git a/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java b/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
index 88e0caa..aa5765c 100644
--- a/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
+++ b/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
@@ -514,6 +514,30 @@
}
/**
+ * new BigDecimal(String value) when value has multiple signs.
+ */
+ public void testConstrStringMultipleSignsStartWithPlus() {
+ String a = "+-3";
+ try {
+ new BigDecimal(a);
+ fail("NumberFormatException expected");
+ } catch (NumberFormatException expected) {
+ }
+ }
+
+ /**
+ * new BigDecimal(String value) when value has multiple signs.
+ */
+ public void testConstrStringMultipleSignsStartWithMinus() {
+ String a = "-+3";
+ try {
+ new BigDecimal(a);
+ fail("NumberFormatException expected");
+ } catch (NumberFormatException expected) {
+ }
+ }
+
+ /**
* new BigDecimal(String value, MathContext).
*/
public void testConstrStringMathContext() {