[LANG-1723]: Throw NumberFormatException instead of IndexOutOfBoundsException in NumberUtils.getMantissa(String, int) (#1145)

* [LANG-1723]: Wrap possible IOOBE with NumberFormatException

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

* Fix unit test

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

* Don't change the formatting

* Don't change the formatting

* Query length once

* Less duplication

---------

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
This commit is contained in:
Arthur Chan 2023-12-09 14:33:15 +00:00 committed by GitHub
parent 26e41e0199
commit 6a65b76f2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -492,11 +492,15 @@ public static Number createNumber(final String str) {
* @param str the string representation of the number
* @param stopPos the position of the exponent or decimal point
* @return mantissa of the given number
* @throws NumberFormatException if no mantissa can be retrieved
*/
private static String getMantissa(final String str, final int stopPos) {
final char firstChar = str.charAt(0);
final boolean hasSign = firstChar == '-' || firstChar == '+';
final int length = str.length();
if (length <= (hasSign ? 1 : 0) || length < stopPos) {
throw new NumberFormatException(str + " is not a valid number.");
}
return hasSign ? str.substring(1, stopPos) : str.substring(0, stopPos);
}

View File

@ -724,6 +724,7 @@ public void testCreateNumberZero() {
@Test
public void testInvalidNumber() {
assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("E123e.3"));
assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("-"));
}
/**