Repository navigation
parser: prevent a stack overflow on deeply nested expressions - #12
Merged
Merged
Conversation
Chains of prefix operators, casts, binary operators, member accesses, calls, and indexers are parsed iteratively, but the expression builder and the ToString methods of the expression nodes process the resulting tree recursively. An input such as 200,000 minus signs followed by a number overflowed the stack and crashed the process. Compute the depth of each node when it is created and stop parsing with a fatal error when the tree is deeper than 256 levels. Check call and indexer chains before HijackMemberExpression processes them recursively. Deeply nested parentheses, arguments, and conditional expressions still recurse in the grammar itself and are not covered by this change.
Deeply nested parentheses, arguments, indexers, and conditional expressions recurse in the grammar itself, so they overflowed the stack before the depth limit could reject them, for example at about 250 nested parentheses on a 1 MB stack. Check the remaining stack each time the grammar re-enters the expression parser and stop parsing with a fatal error when it runs short. Route the false branch of a conditional expression through the same parser, so a single check covers every recursion. The expression builder performs the same check, since it can run on a smaller stack than the parser or on a tree created directly. Describe the limits in the README.
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.
Parsing a long chain or a deeply nested expression could overflow the stack and crash the process, which cannot be caught. For example,
new string('-', 200_000) + "1"crashed in the expression builder, and about 250 nested parentheses crashed in the grammar on a 1 MB stack.This PR limits the depth of the expression tree to 256 levels. Chains such as
a + b + care parsed iteratively, so the parser computes the depth of each node when it creates it and fails with a parse error when the tree is too deep. Parentheses, arguments, indexers, and conditional expressions recurse in the grammar, so the parser also checks the remaining stack each time it re-enters the expression parser. The expression builder performs the same check.Expressions deeper than 256 levels that parsed before now fail. On a small stack, nested expressions can be rejected before the limit: a 1 MB stack fits about 230 nested parentheses on .NET 10. The README describes the limits.