diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 811cbc297..a0f5000c2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + NumberUtils#isNumber(String) fails to reject invalid Octal numbers NumberUtils#isNumber does not allow for hex 0XABCD StringUtils.toEncodedString(byte[], Charset) needlessly throws UnsupportedEncodingException Add APIs MutableBoolean setTrue() and setFalse() diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index f1054eeb2..a871462e5 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -1345,26 +1345,33 @@ public class NumberUtils { boolean foundDigit = false; // deal with any possible sign up front final int start = (chars[0] == '-') ? 1 : 0; - if (sz > start + 1 && chars[start] == '0' - && - ( - (chars[start + 1] == 'x') || - (chars[start + 1] == 'X') - ) - ) { - int i = start + 2; - if (i == sz) { - return false; // str == "0x" - } - // checking hex (it can't be anything else) - for (; i < chars.length; i++) { - if ((chars[i] < '0' || chars[i] > '9') - && (chars[i] < 'a' || chars[i] > 'f') - && (chars[i] < 'A' || chars[i] > 'F')) { - return false; + if (sz > start + 1 && chars[start] == '0') { // leading 0 + if ( + (chars[start + 1] == 'x') || + (chars[start + 1] == 'X') + ) { // leading 0x/0X + int i = start + 2; + if (i == sz) { + return false; // str == "0x" } - } - return true; + // checking hex (it can't be anything else) + for (; i < chars.length; i++) { + if ((chars[i] < '0' || chars[i] > '9') + && (chars[i] < 'a' || chars[i] > 'f') + && (chars[i] < 'A' || chars[i] > 'F')) { + return false; + } + } + return true; + } else { // leading 0, but not hex, must be octal + int i = start + 1; + for (; i < chars.length; i++) { + if (chars[i] < '0' || chars[i] > '7') { + return false; + } + } + return true; + } } sz--; // don't want to loop to the last char, check it afterwords // for type qualifiers diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index d211b6c49..ea895e256 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -1225,6 +1225,15 @@ public class NumberUtilsTest { compareIsNumberWithCreateNumber("1.1L", false); // LANG-664 } + @Test + public void testLANG971() { + compareIsNumberWithCreateNumber("0085", false); + compareIsNumberWithCreateNumber("085", false); + compareIsNumberWithCreateNumber("08", false); + compareIsNumberWithCreateNumber("07", true); + compareIsNumberWithCreateNumber("00", true); + } + @Test public void testLANG972() { compareIsNumberWithCreateNumber("0xABCD", true);