Skip to content

Commit f6f249b

Browse files
authored
Expose ValueException in Grok (#47368)
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.
1 parent e86d40f commit f6f249b

File tree

1 file changed

+6
-10
lines changed
  • libs/grok/src/main/java/org/elasticsearch/grok

1 file changed

+6
-10
lines changed

libs/grok/src/main/java/org/elasticsearch/grok/Grok.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.joni.Regex;
2727
import org.joni.Region;
2828
import org.joni.Syntax;
29-
import org.joni.exception.ValueException;
3029

3130
import java.io.BufferedReader;
3231
import java.io.IOException;
@@ -150,17 +149,14 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
150149
}
151150

152151
public String groupMatch(String name, Region region, String pattern) {
153-
try {
154-
int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0,
155-
name.getBytes(StandardCharsets.UTF_8).length, region);
156-
int begin = region.beg[number];
157-
int end = region.end[number];
158-
return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8);
159-
} catch (StringIndexOutOfBoundsException e) {
160-
return null;
161-
} catch (ValueException e) {
152+
int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0,
153+
name.getBytes(StandardCharsets.UTF_8).length, region);
154+
int begin = region.beg[number];
155+
int end = region.end[number];
156+
if (begin < 0) { // no match found
162157
return null;
163158
}
159+
return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8);
164160
}
165161

166162
/**

0 commit comments

Comments
 (0)