@@ -79,11 +79,7 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
7979 this .namedCaptures = namedCaptures ;
8080 this .matcherWatchdog = matcherWatchdog ;
8181
82- for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
83- String name = entry .getKey ();
84- String pattern = entry .getValue ();
85- forbidCircularReferences (name , new ArrayList <>(), pattern );
86- }
82+ forbidCircularReferences ();
8783
8884 String expression = toRegex (grokPattern );
8985 byte [] expressionBytes = expression .getBytes (StandardCharsets .UTF_8 );
@@ -104,7 +100,8 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
104100 * a reference to another named pattern. This method will navigate to all these named patterns and
105101 * check for a circular reference.
106102 */
107- private void forbidCircularReferences (String patternName , List <String > path , String pattern ) {
103+ private void forbidCircularReferences () {
104+
108105 // first ensure that the pattern bank contains no simple circular references (i.e., any pattern
109106 // containing an immediate reference to itself) as those can cause the remainder of this algorithm
110107 // to recurse infinitely
@@ -114,8 +111,12 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
114111 }
115112 }
116113
117- // next recursively check any other pattern names referenced in the pattern
118- innerForbidCircularReferences (patternName , path , pattern );
114+ // next, recursively check any other pattern names referenced in each pattern
115+ for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
116+ String name = entry .getKey ();
117+ String pattern = entry .getValue ();
118+ innerForbidCircularReferences (name , new ArrayList <>(), pattern );
119+ }
119120 }
120121
121122 private void innerForbidCircularReferences (String patternName , List <String > path , String pattern ) {
@@ -151,7 +152,7 @@ private void innerForbidCircularReferences(String patternName, List<String> path
151152 }
152153 String otherPatternName = pattern .substring (begin , end );
153154 path .add (otherPatternName );
154- forbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
155+ innerForbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
155156 }
156157 }
157158
0 commit comments