@@ -82,11 +82,7 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
8282 this .namedCaptures = namedCaptures ;
8383 this .matcherWatchdog = matcherWatchdog ;
8484
85- for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
86- String name = entry .getKey ();
87- String pattern = entry .getValue ();
88- forbidCircularReferences (name , new ArrayList <>(), pattern );
89- }
85+ forbidCircularReferences ();
9086
9187 String expression = toRegex (grokPattern );
9288 byte [] expressionBytes = expression .getBytes (StandardCharsets .UTF_8 );
@@ -107,7 +103,8 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
107103 * a reference to another named pattern. This method will navigate to all these named patterns and
108104 * check for a circular reference.
109105 */
110- private void forbidCircularReferences (String patternName , List <String > path , String pattern ) {
106+ private void forbidCircularReferences () {
107+
111108 // first ensure that the pattern bank contains no simple circular references (i.e., any pattern
112109 // containing an immediate reference to itself) as those can cause the remainder of this algorithm
113110 // to recurse infinitely
@@ -117,8 +114,12 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
117114 }
118115 }
119116
120- // next recursively check any other pattern names referenced in the pattern
121- innerForbidCircularReferences (patternName , path , pattern );
117+ // next, recursively check any other pattern names referenced in each pattern
118+ for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
119+ String name = entry .getKey ();
120+ String pattern = entry .getValue ();
121+ innerForbidCircularReferences (name , new ArrayList <>(), pattern );
122+ }
122123 }
123124
124125 private void innerForbidCircularReferences (String patternName , List <String > path , String pattern ) {
@@ -154,7 +155,7 @@ private void innerForbidCircularReferences(String patternName, List<String> path
154155 }
155156 String otherPatternName = pattern .substring (begin , end );
156157 path .add (otherPatternName );
157- forbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
158+ innerForbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
158159 }
159160 }
160161
0 commit comments