Skip to content

Fix stack overflow on deeply nested input and panic on broken pipe - #195

Open
Mathjk wants to merge 1 commit into
PaddiM8:masterfrom
Mathjk:fix/parser-depth-and-epipe
Open

Mathjk wants to merge 1 commit into
PaddiM8:masterfrom
Mathjk:fix/parser-depth-and-epipe

Conversation

@Mathjk

@Mathjk Mathjk commented Sep 29, 2026

Copy link
Copy Markdown

Fixes #194.

Bugs

  1. Stack overflow on deeply nested input — kalker with ~5000 nested parens (or deep or/^/unary chains) overflows the native stack and aborts (0xC00000FD on Windows) instead of returning an error. The existing --max-recursion-depth flag only bounds the evaluator; the recursive-descent parser had no limit.
  2. Panic on broken pipe — kalker "1+1" | head -0 (or any early-closing consumer) panics inside println!/print! (stdio.rs).

Fix (+162/-30, 4 files)

  • kalk/src/parser.rs: parse_depth counter on Context, enforced via a with_depth helper around every genuinely recursive descent (parse_comprehension, parse_or, parse_and, parse_comparison, parse_shift, parse_exponent, parse_unary, parse_primary paren/vector/brace). Limit = --max-recursion-depth when set (the flag now covers parsing too), else new DEFAULT_MAX_PARSE_DEPTH = 512 — chosen from measured overflow thresholds (~1,150 parens release / ~250 debug); 500 nests still evaluate fine, and raising the flag lets 5,000 parens parse cleanly.
  • cli/src/main.rs: default_action runs on a scoped 64 MiB-stack worker so the configured limit is reached before the real stack runs out; help text updated.
  • cli/src/output.rs + repl.rs: output via io::Write; BrokenPipe → silent exit(0) (standard CLI convention, same as coreutils); stderr best-effort.

Verified (Windows + Linux)

Input Before After
5,000 nested parens abort 0xC00000FD Operation recursed too deeply., exit 0
500/511 parens, normal exprs (sum, sqrt(9)+2^3, stdin 5*3) correct unchanged
1+1 | head -0 ×8 panic stdio.rs:1166 (racy) silent, exit 0
600-deep or/unary/power chains overflow clean error or result

cargo test 81/0 incl. new test_deeply_nested_expression; fmt/clippy clean; windows-gnu build green.

Note: a ~20,000-term left-associative chain can still overflow in AST eval traversal — pre-existing, separate from this fix; happy to follow up.

The recursive descent parser had no depth limit, so deeply nested
input (e.g. ~5000 nested parentheses) overflowed the stack and
aborted the process instead of returning an error. A parse depth
counter is now tracked in the parser Context and bounded by
max_recursion_depth when set (the existing --max-recursion-depth
flag now applies to parsing as well as evaluation), otherwise by a
new default of 512. Deep input now yields "Operation recursed too
deeply." The CLI also evaluates on a worker thread with a larger
stack so the configured limit is reached before the real stack
limit.

Printing results to a closed pipe (e.g. `kalker "1+1" | head -0`)
panicked in std::io::stdio. Output is now written via explicit
io::Write calls, and a BrokenPipe error exits the process silently
with status 0.
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.

Stack overflow on deeply nested input + panic when stdout pipe closes (EPIPE)

1 participant