@@ -95,11 +95,7 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
95
95
this .namedCaptures = namedCaptures ;
96
96
this .threadWatchdog = threadWatchdog ;
97
97
98
- for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
99
- String name = entry .getKey ();
100
- String pattern = entry .getValue ();
101
- forbidCircularReferences (name , new ArrayList <>(), pattern );
102
- }
98
+ forbidCircularReferences ();
103
99
104
100
String expression = toRegex (grokPattern );
105
101
byte [] expressionBytes = expression .getBytes (StandardCharsets .UTF_8 );
@@ -113,7 +109,8 @@ private Grok(Map<String, String> patternBank, String grokPattern, boolean namedC
113
109
* a reference to another named pattern. This method will navigate to all these named patterns and
114
110
* check for a circular reference.
115
111
*/
116
- private void forbidCircularReferences (String patternName , List <String > path , String pattern ) {
112
+ private void forbidCircularReferences () {
113
+
117
114
// first ensure that the pattern bank contains no simple circular references (i.e., any pattern
118
115
// containing an immediate reference to itself) as those can cause the remainder of this algorithm
119
116
// to recurse infinitely
@@ -123,8 +120,12 @@ private void forbidCircularReferences(String patternName, List<String> path, Str
123
120
}
124
121
}
125
122
126
- // next recursively check any other pattern names referenced in the pattern
127
- innerForbidCircularReferences (patternName , path , pattern );
123
+ // next, recursively check any other pattern names referenced in each pattern
124
+ for (Map .Entry <String , String > entry : patternBank .entrySet ()) {
125
+ String name = entry .getKey ();
126
+ String pattern = entry .getValue ();
127
+ innerForbidCircularReferences (name , new ArrayList <>(), pattern );
128
+ }
128
129
}
129
130
130
131
private void innerForbidCircularReferences (String patternName , List <String > path , String pattern ) {
@@ -160,7 +161,7 @@ private void innerForbidCircularReferences(String patternName, List<String> path
160
161
}
161
162
String otherPatternName = pattern .substring (begin , end );
162
163
path .add (otherPatternName );
163
- forbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
164
+ innerForbidCircularReferences (patternName , path , patternBank .get (otherPatternName ));
164
165
}
165
166
}
166
167
0 commit comments