From 6f2c93ceeecc48f4eca2c52345a7bf33329211c1 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Wed, 6 Dec 2023 14:44:40 +0000 Subject: [PATCH] LANG-1721: Fix wrong number check that cause StringIndexOutOfBoundsException (#1140) * LANG-1721: Fix wrong number check that cause StringIndexOutOfBoundsException Signed-off-by: Arthur Chan * LANG-1721: Fix unit test Signed-off-by: Arthur Chan --------- Signed-off-by: Arthur Chan --- src/main/java/org/apache/commons/lang3/math/NumberUtils.java | 2 +- .../java/org/apache/commons/lang3/math/NumberUtilsTest.java | 5 +++++ 2 files changed, 6 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 11da21db3..03e1b938a 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -361,7 +361,7 @@ public class NumberUtils { final boolean requestType = !Character.isDigit(lastChar) && lastChar != '.'; if (decPos > -1) { // there is a decimal point if (expPos > -1) { // there is an exponent - if (expPos < decPos || expPos > length) { // prevents double exponent causing IOOBE + if (expPos <= decPos || expPos > length) { // prevents double exponent causing IOOBE throw new NumberFormatException(str + " is not a valid number."); } dec = str.substring(decPos + 1, expPos); 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 3e53e1a97..d2a98ab83 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -1743,4 +1743,9 @@ public class NumberUtilsTest extends AbstractLangTest { assertEquals(12345, NumberUtils.toShort("12345", (short) 5), "toShort(String, short) 1 failed"); assertEquals(5, NumberUtils.toShort("1234.5", (short) 5), "toShort(String, short) 2 failed"); } + + @Test + public void testInvalidNumber() { + assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("E123e.3")); + } }