Skip to content

Never crash: strip byte counts near u64::MAX don't overflow their sum in ORF, SRW, ARW and PEF - #3386

Merged
aiongg merged 1 commit into
storytold:mainfrom
Bruno-Alumn:fix/strip-byte-count-sum-overflow
Oct 31, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
Bruno-Alumn:fix/strip-byte-count-sum-overflow

Conversation

@Bruno-Alumn

@Bruno-Alumn Bruno-Alumn commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

The ORF, SRW, ARW and PEF decoders added up their strip byte counts with .sum::<u64>():

  • orf.rs:209
  • srw.rs:100
  • arw.rs:631
  • pef.rs:270 and :277

The classic-TIFF reader accepts LONG8 values (types 16 to 18), so a file whose StripByteCounts are [u64::MAX, 1] overflows that sum. In debug builds that panics in both probe and decode; in release the sum wraps. Each site now uses fold(0u64, |a, c| a.saturating_add(c.len)), as raw/src/lib.rs already does. A saturated sum then fails the decoders' existing size checks as usual.

Tests: strip_byte_counts_past_u64_max_do_not_overflow in vendor::{orf,srw,arw,pef}::tests. Each runs the Header and Full modes. They use a shared helper in vendor.rs tests that rewrites a built one-strip file into two strips with LONG8 counts [u64::MAX, 1]. The PEF test covers both the Huffman (65535) and the uncompressed path. All four panic on main.

Notes:

Checked on macOS (M1 Pro): fmt, clippy, cargo test --workspace --no-fail-fast, parity, layers, assets and wasm. The only failures are the same on main here (445dd31):

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of damaged or specially crafted Olympus ORF, Samsung SRW, Sony ARW, and Pentax PEF files with excessively large strip-size values. These files no longer cause crashes in debug builds; they may be rejected or read only as far as their available data allows.

… in ORF, SRW, ARW and PEF

The TIFF reader accepts LONG8 (type 16) StripByteCounts in classic
TIFF, so a file can declare strips of u64::MAX and 1 bytes. The ORF,
SRW, ARW and PEF decoders added the strip lengths with
Iterator::sum::<u64>(), which panics on overflow in debug builds. They
now add them with a saturating fold, as raw/src/lib.rs already does,
so such a file is refused or read as far as its data go.

The tests rewrite a synthetic file of each decoder into two strips
with LONG8 counts [u64::MAX, 1] (vendor::tests::huge_strip_counts);
PEF is checked for the Huffman-coded and the uncompressed path.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5194cc2 and 1c906ea.

Files selected for processing: 6
  • crates/raw/src/vendor.rs
  • crates/raw/src/vendor/arw.rs
  • crates/raw/src/vendor/orf.rs
  • crates/raw/src/vendor/pef.rs
  • crates/raw/src/vendor/srw.rs
  • docs/whats-new.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Walkthrough

The ARW, ORF, PEF, and SRW decoders now use saturating addition when totaling TIFF strip byte counts. Regression tests exercise totals above u64::MAX in header and full modes. Release notes describe the change.

Changes: TIFF strip totals

Layer / File(s) Summary
Decoder totals and regression coverage
crates/raw/src/vendor.rs, crates/raw/src/vendor/{arw,orf,pef,srw}.rs, docs/whats-new.md
The four decoders now use saturating addition for strip byte counts. Tests construct totals above u64::MAX and invoke decoding in header and full modes. The release notes describe the handling of these files.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • storytold/lightcraft#2561 — This change prevents aggregate u64 overflow in the related decoders, but does not add the issue’s real-slice validation before allocation.

Suggested reviewers: radialmonster

Merge Risk: Minimal · up to 1c906

The targeted overflow is addressed, and no actionable merge-blocking risk is established.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the main change: preventing strip byte-count overflow near u64::MAX in the ORF, SRW, ARW, and PEF decoders.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 9 functions across 5 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

emmertex referenced this pull request in emmertex/chromaforge Oct 30, 2026
* Port upstream #3385: a mask id at the u32 limit no longer overflows

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

* Port upstream #3386: strip byte counts past u64::MAX do not overflow

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

* Port upstream #3384: a SAM 3 checkpoint in Meta's format is named as such

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

* Port upstream #3390: the Flatpak may talk to the accessibility bus

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

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
@aiongg
aiongg merged commit b38bb32 into storytold:main Oct 31, 2026
6 checks passed
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