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