LANG-822: NumberUtils#createNumber - bad behaviour for leading "--"

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1407973 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-11-11 13:16:22 +00:00
parent a7b467a74c
commit 350cf8c2da
3 changed files with 23 additions and 8 deletions

View File

@ -35,6 +35,7 @@
<action issue="LANG-828" type="fix">FastDateParser does not handle non-Gregorian calendars properly</action>
<action issue="LANG-826" type="fix">FastDateParser does not handle non-ASCII digits correctly</action>
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action>
<action issue="LANG-822" type="fix">NumberUtils#createNumber - bad behaviour for leading "--"</action>
<action issue="LANG-818" type="fix">FastDateFormat's "z" pattern does not respect timezone of Calendar instances passed to format()</action>
<action issue="LANG-817" type="fix">Add org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8</action>
<action issue="LANG-813" type="fix">StringUtils.equalsIgnoreCase doesn't check string reference equality</action>

View File

@ -449,13 +449,6 @@ public class NumberUtils {
if (StringUtils.isBlank(str)) {
throw new NumberFormatException("A blank string is not a valid number");
}
if (str.startsWith("--")) {
// this is protection for poorness in java.lang.BigDecimal.
// it accepts this as a legal value, but it does not appear
// to be in specification of class. OS X Java parses it to
// a wrong value.
return null;
}
if (str.startsWith("0x") || str.startsWith("-0x") || str.startsWith("0X") || str.startsWith("-0X")) {
int hexDigits = str.length() - 2; // drop 0x
if (str.startsWith("-")) { // drop -
@ -721,7 +714,14 @@ public class NumberUtils {
// handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException
if (StringUtils.isBlank(str)) {
throw new NumberFormatException("A blank string is not a valid number");
}
}
if (str.trim().startsWith("--")) {
// this is protection for poorness in java.lang.BigDecimal.
// it accepts this as a legal value, but it does not appear
// to be in specification of class. OS X Java parses it to
// a wrong value.
throw new NumberFormatException(str + " is not a valid number.");
}
return new BigDecimal(str);
}

View File

@ -229,6 +229,20 @@ public class NumberUtilsTest {
// LANG-693
assertEquals("createNumber(String) LANG-693 failed", Double.valueOf(Double.MAX_VALUE), NumberUtils
.createNumber("" + Double.MAX_VALUE));
// LANG-822
// ensure that the underlying negative number would create a BigDecimal
final Number bigNum = NumberUtils.createNumber("-1.1E-700F");
assertEquals(BigDecimal.class,bigNum.getClass());
assertNotNull(bigNum);
// Check that the code fails to create a valid number when preceeded by -- rather than -
try {
NumberUtils.createNumber("--1.1E-700F");
fail("Expected NumberFormatException");
} catch (NumberFormatException nfe) {
// expected
}
}
@Test