Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/apache/commons/lang3/time/FastDateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,15 @@ boolean parse(final FastDateParser parser, final Calendar calendar, final String
pos.setErrorIndex(idx);
return false;
}
final int value = Integer.parseInt(source.substring(pos.getIndex(), idx));
final int value;
try {
value = Integer.parseInt(source.substring(pos.getIndex(), idx));
} catch (final NumberFormatException nfe) {
// A run of digits that overflows int cannot be represented by this field; signal a parse failure
// rather than letting NumberFormatException escape the ParsePosition-based parse methods.
pos.setErrorIndex(pos.getIndex());
return false;
}
pos.setIndex(idx);
calendar.set(field, modify(parser, value));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -411,6 +412,20 @@ void testLang1121(final TriFunction<String, TimeZone, Locale, DateParser> dpProv
assertEquals(expected, actual);
}

@ParameterizedTest
@MethodSource(DATE_PARSER_PARAMETERS)
void testLang1359(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider) {
// A trailing numeric field is unbounded, so a digit run that overflows int must fail the parse
// through the ParsePosition/ParseException contract, not escape as a NumberFormatException.
final DateParser fdp = getInstance(dpProvider, "yyyy", TimeZones.GMT, Locale.US);
final String overflow = "99999999999";
assertThrows(ParseException.class, () -> fdp.parse(overflow));
final ParsePosition pos = new ParsePosition(0);
assertNull(fdp.parseObject(overflow, pos));
assertTrue(pos.getErrorIndex() >= 0);
assertEquals(0, pos.getIndex());
}

@ParameterizedTest
@MethodSource(DATE_PARSER_PARAMETERS)
void testLang1380(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider) throws ParseException {
Expand Down
Loading