Skip to content

fix(parser): stop runaway nesting, long else chains and huge expressions - #3750

Merged
BaseMax merged 3 commits into
mainfrom
fix/parser-depth
Nov 21, 2026
Merged

BaseMax merged 3 commits into
mainfrom
fix/parser-depth

Conversation

@BaseMax

@BaseMax BaseMax commented Nov 21, 2026

Copy link
Copy Markdown
Member

Three parser problems with deep or long input:

  • An array literal nested past the 128-level limit sent parse_array_lit into a loop that never consumed a token and ran until out of memory. The loop now stops when it makes no progress or after an error. I checked the other list loops (call args, params, generics, struct literals, patterns, match/switch/blocks, extern/export, layout). They already stop on a comma miss, on p.panic or through p_ensure_progress.
  • Each else cond: arm recursed through parse_if_tail and used up the shared depth budget, so a flat chain of about 126 arms failed with "expression nested too deeply". The statement and top-level else chains are now parsed in a loop. Sema still recurses over the chain and segfaulted between 700 and 850 arms, so chains over 256 arms get a clean error.
  • A left-deep chain such as 1 + 1 + ... parsed fine, then sema overflowed the stack (about 1100 terms on an 8 MB stack, about 998 on 6 MB). The parser now tracks expression height and reports "expression is too long" above 512.

Tested with a stage-1 build: the new errors tests (array nesting, 601-term chain, 260-arm chain) and the 200-arm positive test pass, along with the existing depth, if/else and condcomp tests. Height 510 checks clean on a 4 MB stack, and 256 arms run on the C and LLVM backends. check compiler/main.salam is clean, and the review repros (130 nested arrays, 126 arms, 10000-term chains) now give errors instead of OOM or a segfault.

Not fixed here: the C backend sends clang into a crash on int arithmetic chains of about 250 terms. That happens with main today too.

An array literal at the depth limit looped without consuming tokens until it ran out of memory. Else arms now parse in a loop instead of recursing, and very long expressions or chains get a clean error before later passes overflow the stack.
@BaseMax
BaseMax requested a review from a team as a code owner November 21, 2026 09:31
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 24 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4657ccfc-d492-4d28-bbc1-c25c3fb51ad6
Commits

Reviewing files that changed from the base of the PR and between 1796e8b and b6c272d.

Files ignored due to path filters: 1

  • tests/en/general/else_chain_200_arms.out is excluded by !**/*.out
Files selected for processing: 10

  • compiler/i18n.salam
  • compiler/parser/parser.salam
  • compiler/parser/parser_api.salam
  • compiler/parser/parser_expr.salam
  • compiler/parser/parser_expr_primary.salam
  • compiler/parser/parser_stmt.salam
  • tests/en/errors/array_literal_too_deep.salam
  • tests/en/errors/expression_too_long.salam
  • tests/en/errors/if_chain_too_many_arms.salam
  • tests/en/general/else_chain_200_arms.salam
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor
sha=b6c272dfa6398a04e7a274b864755ef2f7329c4e

🤖 Salam AI review

Verdict: 🛠️ Changes needed before merging

commit b6c272d · 11 files · +1096 −49 · model salam

Summary

This pull request hardens the parser against pathological inputs by stopping runaway array literal parsing, converting recursive if/else tail parsing into an iterative loop with a 256-arm limit, and rejecting left-deep expressions taller than 512 nodes. The approach addresses the reported OOM and stack-overflow cases, but there are a few parser-hygiene and convention issues to resolve before merging.

What changed

The parser now defines PS_MAX_EXPR_HEIGHT and PS_MAX_IF_ARMS in compiler/parser/parser.salam, adds expr_h to parser state, and uses these limits in expression and if/else parsing. parse_array_lit in compiler/parser/parser_expr_primary.salam breaks when parsing makes no progress or the parser is already in panic, preventing the reported infinite loop. parse_if_tail and parse_toplevel_if_tail were rewritten as loops that link arms iteratively and emit a clean error when a chain exceeds 256 arms. parse_expr_bp tracks expression height across the Pratt loop and reports an error for overly long expressions. The PR also adds error and positive regression tests, while compiler/i18n.salam was modified but its diff was not shown.

Review

  • compiler/parser/parser_stmt.salam and compiler/parser/parser_api.salam, over-limit arm handling: When arms.Len() >= PS_MAX_IF_ARMS, the code reports the error but still parses the arm into n without pushing it into arms, linking it, or finalizing it. That leaves extra AST nodes unfinalized and unattached. Consider finalizing or otherwise handling these nodes explicitly so error recovery does not violate AST invariants.
  • tests/en/errors/array_literal_too_deep.salam, tests/en/errors/expression_too_long.salam, tests/en/errors/if_chain_too_many_arms.salam, tests/en/general/else_chain_200_arms.salam: The added Salam test files shown in the diff do not include a // lang: en header at the top. If the repository requires the language header on every source file, these tests should add it.
  • compiler/i18n.salam: The file is listed as modified, but no diff was provided. What changed there, and does it add the new diagnostic text for both English and Persian?

Tests

The PR adds targeted coverage for deep array nesting, overly long expressions, too many if/else arms, and a 200-arm positive case. The description reports stage-1 checks and backend runs; I did not run builds or tests. Because the if-chain and long-expression tests are truncated in the diff, I could not verify their full contents.

Risks

The new fixed limits protect parser and sema from stack exhaustion, but they also introduce user-visible ceilings; the chosen values should match the project's compatibility expectations. The PR notes an existing C backend/clang crash around ~250-term int arithmetic chains, so the new expression-length diagnostic reduces one failure mode but does not eliminate all large-expression backend risks.


⚠️ This review was written automatically by an AI and may contain mistakes. It is a helper for maintainers, not a replacement for a human review: check the code yourself before merging.

@BaseMax

BaseMax commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Review notes: the new tests follow the existing tests/en/errors convention (only 1 of 554 of those files has a lang header, so none is added). Arms past the 256 limit are parsed into arena nodes that are never linked, but a parse error has been reported by then so compilation stops before they are used and nothing is leaked. compiler/i18n.salam adds the two new diagnostics in both English and Persian.

@BaseMax
BaseMax merged commit fcf574a into main Nov 21, 2026
15 checks passed
@BaseMax
BaseMax deleted the fix/parser-depth branch November 21, 2026 10:01
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