File tree 2 files changed +20
-0
lines changed
Sources/_RegexParser/Regex/Parse
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -523,6 +523,19 @@ extension Parser {
523
523
mutating func parseCustomCharacterClass(
524
524
_ start: Source . Located < CustomCC . Start >
525
525
) -> CustomCC {
526
+ // Excessively nested recursion is a common DOS attack, so limit
527
+ // our recursion.
528
+ context. parseDepth += 1
529
+ defer { context. parseDepth -= 1 }
530
+ guard context. parseDepth < context. maxParseDepth else {
531
+ self . errorAtCurrentPosition ( . nestingTooDeep)
532
+
533
+ // This is not generally recoverable and further errors will be
534
+ // incorrect
535
+ diags. suppressFurtherDiagnostics = true
536
+ return . init( start, [ ] , start. location)
537
+ }
538
+
526
539
let alreadyInCCC = context. isInCustomCharacterClass
527
540
context. isInCustomCharacterClass = true
528
541
defer { context. isInCustomCharacterClass = alreadyInCCC }
Original file line number Diff line number Diff line change @@ -3335,6 +3335,13 @@ extension RegexTests {
3335
3335
+ " a "
3336
3336
+ String( repeating: " )* " , count: 500 ) ,
3337
3337
. nestingTooDeep)
3338
+
3339
+ diagnosticTest (
3340
+ String ( repeating: " [ " , count: 500 )
3341
+ + " a "
3342
+ + String( repeating: " ]* " , count: 500 ) ,
3343
+ . nestingTooDeep)
3344
+
3338
3345
}
3339
3346
3340
3347
func testDelimiterLexingErrors( ) {
You can’t perform that action at this time.
0 commit comments