LANG-971 NumberUtils#isNumber(String) fails to reject invalid Octal numbers

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1566967 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2014-02-11 02:53:03 +00:00
parent 5a509030a9
commit 9397608dd3
3 changed files with 36 additions and 19 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.3" date="TBA" description="Bugfix and Feature release"> <release version="3.3" date="TBA" description="Bugfix and Feature release">
<action issue="LANG-971" type="fix" dev="sebb">NumberUtils#isNumber(String) fails to reject invalid Octal numbers</action>
<action issue="LANG-972" type="fix" dev="sebb">NumberUtils#isNumber does not allow for hex 0XABCD</action> <action issue="LANG-972" type="fix" dev="sebb">NumberUtils#isNumber does not allow for hex 0XABCD</action>
<action issue="LANG-969" type="fix" dev="ggregory" due-to="Matt Bishop">StringUtils.toEncodedString(byte[], Charset) needlessly throws UnsupportedEncodingException</action> <action issue="LANG-969" type="fix" dev="ggregory" due-to="Matt Bishop">StringUtils.toEncodedString(byte[], Charset) needlessly throws UnsupportedEncodingException</action>
<action issue="LANG-970" type="add" dev="ggregory">Add APIs MutableBoolean setTrue() and setFalse()</action> <action issue="LANG-970" type="add" dev="ggregory">Add APIs MutableBoolean setTrue() and setFalse()</action>

View File

@ -1345,26 +1345,33 @@ public static boolean isNumber(final String str) {
boolean foundDigit = false; boolean foundDigit = false;
// deal with any possible sign up front // deal with any possible sign up front
final int start = (chars[0] == '-') ? 1 : 0; final int start = (chars[0] == '-') ? 1 : 0;
if (sz > start + 1 && chars[start] == '0' if (sz > start + 1 && chars[start] == '0') { // leading 0
&& if (
( (chars[start + 1] == 'x') ||
(chars[start + 1] == 'x') || (chars[start + 1] == 'X')
(chars[start + 1] == 'X') ) { // leading 0x/0X
) int i = start + 2;
) { if (i == sz) {
int i = start + 2; return false; // str == "0x"
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;
} }
} // checking hex (it can't be anything else)
return true; 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 sz--; // don't want to loop to the last char, check it afterwords
// for type qualifiers // for type qualifiers

View File

@ -1225,6 +1225,15 @@ public void testIsNumber() {
compareIsNumberWithCreateNumber("1.1L", false); // LANG-664 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 @Test
public void testLANG972() { public void testLANG972() {
compareIsNumberWithCreateNumber("0xABCD", true); compareIsNumberWithCreateNumber("0xABCD", true);