Repository navigation
fix(parser): stop runaway nesting, long else chains and huge expressions - #3750
Conversation
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.
|
Warning Review limit reachedYou'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. View limit details
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. |
sha=b6c272dfa6398a04e7a274b864755ef2f7329c4e
🤖 Salam AI reviewVerdict: 🛠️ Changes needed before merging commit SummaryThis 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 changedThe parser now defines Review
TestsThe 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. RisksThe 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.
|
|
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. |
Three parser problems with deep or long input:
parse_array_litinto 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, onp.panicor throughp_ensure_progress.else cond:arm recursed throughparse_if_tailand 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.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.salamis 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.