@@ -15,6 +15,7 @@ pub struct RegexOptions {
15
15
pub pats : Vec < String > ,
16
16
pub size_limit : usize ,
17
17
pub dfa_size_limit : usize ,
18
+ pub nest_limit : u32 ,
18
19
pub case_insensitive : bool ,
19
20
pub multi_line : bool ,
20
21
pub dot_matches_new_line : bool ,
@@ -29,6 +30,7 @@ impl Default for RegexOptions {
29
30
pats : vec ! [ ] ,
30
31
size_limit : 10 * ( 1 <<20 ) ,
31
32
dfa_size_limit : 2 * ( 1 <<20 ) ,
33
+ nest_limit : 250 ,
32
34
case_insensitive : false ,
33
35
multi_line : false ,
34
36
dot_matches_new_line : false ,
@@ -163,6 +165,36 @@ impl RegexBuilder {
163
165
self . 0 . dfa_size_limit = limit;
164
166
self
165
167
}
168
+
169
+ /// Set the nesting limit for this parser.
170
+ ///
171
+ /// The nesting limit controls how deep the abstract syntax tree is allowed
172
+ /// to be. If the AST exceeds the given limit (e.g., with too many nested
173
+ /// groups), then an error is returned by the parser.
174
+ ///
175
+ /// The purpose of this limit is to act as a heuristic to prevent stack
176
+ /// overflow for consumers that do structural induction on an `Ast` using
177
+ /// explicit recursion. While this crate never does this (instead using
178
+ /// constant stack space and moving the call stack to the heap), other
179
+ /// crates may.
180
+ ///
181
+ /// This limit is not checked until the entire Ast is parsed. Therefore,
182
+ /// if callers want to put a limit on the amount of heap space used, then
183
+ /// they should impose a limit on the length, in bytes, of the concrete
184
+ /// pattern string. In particular, this is viable since this parser
185
+ /// implementation will limit itself to heap space proportional to the
186
+ /// lenth of the pattern string.
187
+ ///
188
+ /// Note that a nest limit of `0` will return a nest limit error for most
189
+ /// patterns but not all. For example, a nest limit of `0` permits `a` but
190
+ /// not `ab`, since `ab` requires a concatenation, which results in a nest
191
+ /// depth of `1`. In general, a nest limit is not something that manifests
192
+ /// in an obvious way in the concrete syntax, therefore, it should not be
193
+ /// used in a granular way.
194
+ pub fn nest_limit( & mut self , limit: u32 ) -> & mut RegexBuilder {
195
+ self . 0 . nest_limit = limit;
196
+ self
197
+ }
166
198
}
167
199
}
168
200
}
@@ -274,6 +306,37 @@ impl RegexSetBuilder {
274
306
self . 0 . dfa_size_limit = limit;
275
307
self
276
308
}
309
+
310
+ /// Set the nesting limit for this parser.
311
+ ///
312
+ /// The nesting limit controls how deep the abstract syntax tree is allowed
313
+ /// to be. If the AST exceeds the given limit (e.g., with too many nested
314
+ /// groups), then an error is returned by the parser.
315
+ ///
316
+ /// The purpose of this limit is to act as a heuristic to prevent stack
317
+ /// overflow for consumers that do structural induction on an `Ast` using
318
+ /// explicit recursion. While this crate never does this (instead using
319
+ /// constant stack space and moving the call stack to the heap), other
320
+ /// crates may.
321
+ ///
322
+ /// This limit is not checked until the entire Ast is parsed. Therefore,
323
+ /// if callers want to put a limit on the amount of heap space used, then
324
+ /// they should impose a limit on the length, in bytes, of the concrete
325
+ /// pattern string. In particular, this is viable since this parser
326
+ /// implementation will limit itself to heap space proportional to the
327
+ /// lenth of the pattern string.
328
+ ///
329
+ /// Note that a nest limit of `0` will return a nest limit error for most
330
+ /// patterns but not all. For example, a nest limit of `0` permits `a` but
331
+ /// not `ab`, since `ab` requires a concatenation, which results in a nest
332
+ /// depth of `1`. In general, a nest limit is not something that manifests
333
+ /// in an obvious way in the concrete syntax, therefore, it should not be
334
+ /// used in a granular way.
335
+ pub fn nest_limit( & mut self , limit: u32 ) -> & mut RegexSetBuilder {
336
+ self . 0 . nest_limit = limit;
337
+ self
338
+ }
339
+
277
340
}
278
341
}
279
342
}
0 commit comments