Skip to content

Commit

Permalink
Expose ValueException in Grok (#47368)
Browse files Browse the repository at this point in the history
Previously, Grok's groupMatch would allow the code to
fall into an IndexOutOfBoundsException, which can be avoided.
The other exception that can come up is a ValueException. The times
this exception occurs is less understood, but it may make sense to expose
this since it typically means something did not go well.
  • Loading branch information
talevy authored Oct 4, 2019
1 parent e86d40f commit f6f249b
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions libs/grok/src/main/java/org/elasticsearch/grok/Grok.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.joni.Regex;
import org.joni.Region;
import org.joni.Syntax;
import org.joni.exception.ValueException;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -150,17 +149,14 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
}

public String groupMatch(String name, Region region, String pattern) {
try {
int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0,
name.getBytes(StandardCharsets.UTF_8).length, region);
int begin = region.beg[number];
int end = region.end[number];
return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8);
} catch (StringIndexOutOfBoundsException e) {
return null;
} catch (ValueException e) {
int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0,
name.getBytes(StandardCharsets.UTF_8).length, region);
int begin = region.beg[number];
int end = region.end[number];
if (begin < 0) { // no match found
return null;
}
return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8);
}

/**
Expand Down

0 comments on commit f6f249b

Please sign in to comment.