mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-08 19:14:52 +00:00
[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:
parent
26e41e0199
commit
6a65b76f2a
@ -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);
|
||||
}
|
||||
|
||||
|
@ -724,6 +724,7 @@ public void testCreateNumberZero() {
|
||||
@Test
|
||||
public void testInvalidNumber() {
|
||||
assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("E123e.3"));
|
||||
assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("-"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user