From c8e61afdb89c58ea8ffaf04593da41ff0888d30e Mon Sep 17 00:00:00 2001 From: Piotr Kosmala Date: Sat, 10 Feb 2018 15:19:36 +0100 Subject: [PATCH] LANG-1060: NumberUtils.isNumber assumes number starting with Zero is octal (closes #313) --- .../java/org/apache/commons/lang3/math/NumberUtils.java | 2 +- .../org/apache/commons/lang3/math/NumberUtilsTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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 5942db7f3..9428ba797 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -1407,7 +1407,7 @@ public class NumberUtils { boolean foundDigit = false; // deal with any possible sign up front final int start = chars[0] == '-' || chars[0] == '+' ? 1 : 0; - if (sz > start + 1 && chars[start] == '0') { // leading 0 + if (sz > start + 1 && chars[start] == '0' && !StringUtils.contains(str, '.')) { // leading 0, skip if is a decimal number if (chars[start + 1] == 'x' || chars[start + 1] == 'X') { // leading 0x/0X int i = start + 2; if (i == sz) { 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 20d87fe30..376a57916 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -1326,6 +1326,10 @@ public class NumberUtilsTest { compareIsNumberWithCreateNumber("-1234", true); compareIsNumberWithCreateNumber("-1234.5", true); compareIsNumberWithCreateNumber("-.12345", true); + compareIsNumberWithCreateNumber("-0001.12345", true); + compareIsNumberWithCreateNumber("-000.12345", true); + compareIsNumberWithCreateNumber("+00.12345", true); + compareIsNumberWithCreateNumber("+0002.12345", true); compareIsNumberWithCreateNumber("-1234E5", true); compareIsNumberWithCreateNumber("0", true); compareIsNumberWithCreateNumber("-0", true); @@ -1342,6 +1346,7 @@ public class NumberUtilsTest { compareIsNumberWithCreateNumber(" ", false); compareIsNumberWithCreateNumber("\r\n\t", false); compareIsNumberWithCreateNumber("--2.3", false); + compareIsNumberWithCreateNumber(".12.3", false); compareIsNumberWithCreateNumber("-123E", false); compareIsNumberWithCreateNumber("-123E+-212", false); @@ -1352,6 +1357,8 @@ public class NumberUtilsTest { compareIsNumberWithCreateNumber("-0ABC123", false); compareIsNumberWithCreateNumber("123.4E-D", false); compareIsNumberWithCreateNumber("123.4ED", false); + compareIsNumberWithCreateNumber("+000E.12345", false); + compareIsNumberWithCreateNumber("-000E.12345", false); compareIsNumberWithCreateNumber("1234E5l", false); compareIsNumberWithCreateNumber("11a", false); compareIsNumberWithCreateNumber("1a", false);