xls: fix panic on a PtgExp token with a truncated operand - #707
Merged
Conversation
jmcnamara
requested changes
Aug 14, 2026
Collaborator
|
Thanks for the fix. I have made some comments inline. Please address them and rebase and force push the changes to your branch. Then mark the comments as "resolved". Also, reduce the commit message down to 1-2 lines max. |
Report the short token as XlsError::Len instead of slicing past the end of rgce.
Butch78
force-pushed
the
fix/ptgexp-truncated-operand
branch
from
August 15, 2026 12:09
39f66c5 to
976372a
Compare
Contributor
Author
|
Added all your suggestions, thanks for everything you do with Calamine! :) |
Collaborator
|
Merged. Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Current behavior
parse_formulaadvances past aPtgExptoken by slicing four bytes offrgcewithout checking that four bytes remain:A workbook whose
PtgExpoperand is truncated panics:Because formulas are parsed while the workbook is read, this aborts
open_workbookitself — the file cannot be opened at all, let alone have its formulas inspected. Any caller handing calamine a file it did not produce (an upload, a corpus, a user attachment) is one malformed workbook away from losing the thread.Expected behavior
A truncated token is malformed input, not a bug in the caller, so it should surface as an
Errlike the reader's other length violations — and the rest of the workbook should still load.Sample code to reproduce
The fix
Report the short token as
XlsError::Len, matching how the reader already handles other length violations (e.g. theFormularecord check atsrc/xls.rs:640):With the fix the attached workbook opens and yields real data — a 23×10 range and 22 formula cells — so this recovers the file rather than merely avoiding the crash. That is what the test asserts.
Test file
tests/ptgexp-truncated-operand.xls, from the public Enron/EDRM corpus via SheetJS. It is the only file in a 2000-workbook sample that triggers this, which is presumably why it has not been reported before.Test added to
tests/test.rsin the existing style; it panics without the fix and passes with it.Environment
Checks
cargo fmt -- --check,cargo clippy --all-targets --all-features -- -Dwarnings,cargo test --all-features, andcargo check --target wasm32-unknown-unknown --all-featuresall pass locally.A note on the wider pattern
Every arm of
parse_formulaadvancesrgcewith an unchecked slice, so this class of panic is not exhausted — #270, #447, #600, #612 and #643 were each the same shape in a different token. I have kept this PR to the one token I can demonstrate with a fixture rather than sweeping the function, since a broad change to a parser I do not maintain is harder to review. Happy to follow up with a systematic guard if you would find that useful.