@@ -79,11 +79,7 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
79
79
this .namedCaptures = namedCaptures ;
80
80
this .matcherWatchdog = matcherWatchdog ;
81
81
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 ();
87
83
88
84
String expression = toRegex (grokPattern );
89
85
byte [] expressionBytes = expression .getBytes (StandardCharsets .UTF_8 );
@@ -104,7 +100,8 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
104
100
* a reference to another named pattern. This method will navigate to all these named patterns and
105
101
* check for a circular reference.
106
102
*/
107
- private void forbidCircularReferences (String patternName , List <String > path , String pattern ) {
103
+ private void forbidCircularReferences () {
104
+
108
105
// first ensure that the pattern bank contains no simple circular references (i.e., any pattern
109
106
// containing an immediate reference to itself) as those can cause the remainder of this algorithm
110
107
// to recurse infinitely
@@ -114,8 +111,12 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
114
111
}
115
112
}
116
113
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
+ }
119
120
}
120
121
121
122
private void innerForbidCircularReferences (String patternName , List <String > path , String pattern ) {
@@ -151,7 +152,7 @@ private void innerForbidCircularReferences(String patternName, List<String> path
151
152
}
152
153
String otherPatternName = pattern .substring (begin , end );
153
154
path .add (otherPatternName );
154
- forbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
155
+ innerForbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
155
156
}
156
157
}
157
158
0 commit comments