Skip to content

Commit cb520d7

Browse files
committed
fix index out of bounds
1 parent 439c350 commit cb520d7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/DateUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public static ZonedDateTime asDateTime(String dateFormat) {
111111

112112
public static ZonedDateTime ofEscapedLiteral(String dateFormat) {
113113
int separatorIdx = dateFormat.lastIndexOf('-') + 3;
114-
if (dateFormat.charAt(separatorIdx) == 'T') {
114+
// Avoid index out of bounds - it will lead to DateTimeParseException anyways
115+
if (separatorIdx >= dateFormat.length() || dateFormat.charAt(separatorIdx) == 'T') {
115116
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_T_LITERAL.withZone(UTC));
116117
} else {
117118
return ZonedDateTime.parse(dateFormat, DATE_TIME_ESCAPED_LITERAL_FORMATTER_WHITESPACE.withZone(UTC));

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/EscapedFunctionsTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,21 @@ public void testTimestampLiteral() {
250250
}
251251

252252
public void testTimestampLiteralValidation() {
253-
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral("2012-01-01_AB 10:01:02.3456"));
253+
String date = buildDate();
254+
ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral(date+ "_AB 10:01:02.3456"));
254255
assertEquals(
255-
"line 1:2: Invalid timestamp received; Text '2012-01-01_AB 10:01:02.3456' could not be parsed at index 10",
256+
"line 1:2: Invalid timestamp received; Text '" + date + "_AB 10:01:02.3456' could not be parsed at index " +
257+
date.length(),
256258
ex.getMessage());
257259
ex = expectThrows(ParsingException.class, () -> timestampLiteral("20120101_AB 10:01:02.3456"));
258260
assertEquals(
259261
"line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0",
260262
ex.getMessage());
263+
264+
ex = expectThrows(ParsingException.class, () -> timestampLiteral(date));
265+
assertEquals(
266+
"line 1:2: Invalid timestamp received; Text '" + date + "' could not be parsed at index " + date.length(),
267+
ex.getMessage());
261268
}
262269

263270
public void testGUID() {

0 commit comments

Comments
 (0)