Sfoglia il codice sorgente

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.
Tal Levy 6 anni fa
parent
commit
f6f249be15
1 ha cambiato i file con 6 aggiunte e 10 eliminazioni
  1. 6 10
      libs/grok/src/main/java/org/elasticsearch/grok/Grok.java

+ 6 - 10
libs/grok/src/main/java/org/elasticsearch/grok/Grok.java

@@ -26,7 +26,6 @@ import org.joni.Option;
 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;
@@ -150,17 +149,14 @@ public final class Grok {
     }
 
     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);
     }
 
     /**