Skip to content

Formula parser: Excel's 8,192-character limit and postfix nesting (never crash) - #1330

Merged
skylartaylor merged 1 commit into
storytold:mainfrom
nihilist-narwhal:fix/formula-length-and-postfix-nesting
Oct 15, 2026
Merged

skylartaylor merged 1 commit into
storytold:mainfrom
nihilist-narwhal:fix/formula-length-and-postfix-nesting

Conversation

@nihilist-narwhal

@nihilist-narwhal nihilist-narwhal commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

A hostile file could abort any process that opens it, GridCraft included: a cell formula of about a million % (=1%%%…) or a 200,000-term sum (=1+1+…) parsed into trees deep enough to overflow the stack when printed, saved, evaluated or dropped — even in release builds. Postfix operators (%, invoking a result) built their nesting in a loop that the 256-level limit did not see.

  • parse refuses formula text longer than Excel's 8,192 characters (MAX_FORMULA_LEN, counted in characters, the leading = excluded). This also bounds the depth of the left-deep trees long operator chains build.
  • Each postfix % and each call of a result now counts towards the nesting limit.
  • A formula from a file that is refused keeps its text and cached value, as other unparsable formulas do.

Part of #1307 (finding 1). Covers the binary, postfix and invocation chains from #1001; the recursive stripping of accounting parentheses in parse_numeric, also listed there, is not changed here.

Tests

  • formula: hostile_formulas_are_errors_not_stack_overflows (small-stack thread: long %, invoke and operator chains are errors) and formulas_longer_than_excel_allows_are_refused (the limit counts characters, not bytes).
  • xlsx: hostile_formula_text_does_not_overflow_the_stack — open, save and drop a workbook with such formulas and a defined name on a 1 MiB stack.
  • Both regression tests abort with a stack overflow without the fix.
  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace, and cargo xtask assets / layers / parity --check / keytips --check pass. cargo xtask ci (fmt, clippy, test, assets, layers, wasm, parity, keytips) passes in CI.

…ver crash)

A hostile file could abort any process that opens it: a cell formula of a
million `%` (`1%%%…`) or a 200,000-term sum parsed into trees deep enough to
overflow the stack when printed, saved, evaluated or dropped, even in release
builds. Postfix operators (`%`, invoking a result) built their nesting in a
loop that the 256-level limit did not see.

- `parse` refuses formula text longer than Excel's 8,192 characters
  (`MAX_FORMULA_LEN`, counted in characters, the leading `=` excluded). This
  also bounds the depth of the left-deep trees long operator chains build.
- Each postfix `%` and each call of a result counts towards the nesting limit.
- A file's formula that is refused keeps its text and cached value, as other
  unparsable formulas do.

Regression tests: formula (small-stack thread: long `%`, invoke and operator
chains are errors; the limit counts characters, not bytes) and xlsx
(`hostile_formula_text_does_not_overflow_the_stack`: open, save and drop a
workbook with such formulas and a defined name on a 1 MiB stack). Both abort
with a stack overflow without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skylartaylor

Copy link
Copy Markdown
Member

Thanks @nihilist-narwhal! Nice targeted fix with good hostile-input coverage. Merged. Thanks also @PierrunoYT and @midasmatterhorn for the reports and follow-up audit. The broader AST/accounting-number recursion cases in #1001 still need separate work.

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.

2 participants