LANG-1721: Fix wrong number check that cause StringIndexOutOfBoundsException (#1140)

* LANG-1721: Fix wrong number check that cause StringIndexOutOfBoundsException

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

* LANG-1721: Fix unit test

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

---------

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
This commit is contained in:
Arthur Chan 2023-12-06 14:44:40 +00:00 committed by GitHub
parent d7e927deff
commit 6f2c93ceee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -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);

View File

@ -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"));
}
}