Skip to content

Commit

Permalink
fix index out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
matriv committed Feb 8, 2020
1 parent 439c350 commit cb520d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public static ZonedDateTime asDateTime(String dateFormat) {

public static ZonedDateTime ofEscapedLiteral(String dateFormat) {
int separatorIdx = dateFormat.lastIndexOf('-') + 3;
if (dateFormat.charAt(separatorIdx) == 'T') {
// Avoid index out of bounds - it will lead to DateTimeParseException anyways
if (separatorIdx >= dateFormat.length() || dateFormat.charAt(separatorIdx) == 'T') {
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_T_LITERAL.withZone(UTC));
} else {
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_WHITESPACE.withZone(UTC));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,21 @@ public void testTimestampLiteral() {
}

public void testTimestampLiteralValidation() {
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral("2012-01-01_AB 10:01:02.3456"));
String date = buildDate();
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral(date+ "_AB 10:01:02.3456"));
assertEquals(
"line 1:2: Invalid timestamp received; Text '2012-01-01_AB 10:01:02.3456' could not be parsed at index 10",
"line 1:2: Invalid timestamp received; Text '" + date + "_AB 10:01:02.3456' could not be parsed at index " +
date.length(),
ex.getMessage());
ex = expectThrows(ParsingException.class, () -> timestampLiteral("20120101_AB 10:01:02.3456"));
assertEquals(
"line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0",
ex.getMessage());

ex = expectThrows(ParsingException.class, () -> timestampLiteral(date));
assertEquals(
"line 1:2: Invalid timestamp received; Text '" + date + "' could not be parsed at index " + date.length(),
ex.getMessage());
}

public void testGUID() {
Expand Down

0 comments on commit cb520d7

Please sign in to comment.