Skip to content

parser: prevent a stack overflow on deeply nested expressions - #12

Merged
rameel merged 3 commits into
mainfrom
parser/prevent-stack-overflow
Nov 17, 2026
Merged

rameel merged 3 commits into
mainfrom
parser/prevent-stack-overflow

Conversation

@rameel

@rameel rameel commented Nov 17, 2026

Copy link
Copy Markdown
Owner

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 + c are 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.

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.
@rameel
rameel merged commit 8a0da4f into main Nov 17, 2026
1 check passed
@rameel
rameel deleted the parser/prevent-stack-overflow branch November 17, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant