Fix StackOverflowException from deeply nested character class subtractions#32
Closed
danmoseley wants to merge 2 commits intomainfrom
Closed
Fix StackOverflowException from deeply nested character class subtractions#32danmoseley wants to merge 2 commits intomainfrom
danmoseley wants to merge 2 commits intomainfrom
Conversation
…tions Convert recursive methods to iterative to prevent stack overflow when parsing and evaluating regex patterns with deeply nested character class subtractions (e.g., [a-[a-[a-[...[a]...]]]]). The following methods were converted from recursive to iterative: - RegexParser.ScanCharClass: uses an explicit parent stack to track nested character class levels during parsing - RegexCharClass.ToStringClass: tail-call converted to do/while loop - RegexCharClass.CharInClassRecursive: converted to toggle-based loop that counts consecutive matching levels - RegexCharClass.ParseRecursive: converted to forward iterative construction of the subtraction chain Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Rename CharInClassRecursive -> CharInClassIterative and ParseRecursive -> ParseIterative since implementations are now iterative - Inline ParseIterative into Parse and ToStringClass(ref) into ToStringClass() - Inline StressTestNestingDepth constant into its only usage - Add tests: case-insensitive subtraction, negated outer class, 4-level nesting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix StackOverflowException from deeply nested character class subtractions
Summary
RegexParser.ScanCharClassand several methods inRegexCharClassrecursively process character class subtractions ([a-[b-[c-[...]]]]). A pattern with ~10,000 nesting levels (~50KB) exhausts the thread stack, causing an uncatchableStackOverflowExceptionthat terminates the process. This is a DoS vector for any service accepting user-supplied regex patterns.All regex modes are affected (
None,Compiled,NonBacktracking) because the crash occurs during parsing, before engine-specific logic.Changes
Converted all recursive subtraction processing to iterative:
RegexParser.ScanCharClass: Uses an explicitList<RegexCharClass?>parent stack. When a subtraction[is encountered, the current char class is pushed and a new one started. When]closes a level, the child is set as the parent's subtraction.RegexCharClass.ToStringClass: Tail-call converted to ado/whileloop over_subtractor.RegexCharClass.CharInClassRecursive: Converted to a toggle-basedwhileloop — each consecutive level where the character matches toggles the result.RegexCharClass.ParseRecursive: Converted to forward iterative construction of the subtraction chain.Character class subtraction was the only recursive parsing path in
RegexParser. Other nestable constructs such as groups ((((...)))) are already parsed iteratively via an explicit stack (PushGroup/PopGroup) and are not susceptible to stack overflow regardless of nesting depth.Tests
CharClassSubtraction_DeepNesting_DoesNotStackOverflow: 10,000 depth (1,000 for SourceGenerated to avoid overwhelming Roslyn). Verifies parsing + matching succeeds.CharClassSubtraction_Correctness: Validates[a-z-[d-w]]and[a-z-[d-w-[m]]]produce correct match/non-match results across all engines.