Skip to content

Add strip offsets and byte counts as uint32_t in GetImageData, as C++ does - #38

Open
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:strip-sums-wrap
Open

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:strip-sums-wrap

Conversation

@kunpai

@kunpai kunpai commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Add strip offsets and byte counts as uint32_t in GetImageData, as C++ does

Problem

GetImageData (C++, Rust) finds a directory's image. For a strip image, it checks that each strip starts where the previous one ends, and adds up the byte counts to get the image's length. C++ does both in uint32_t, so the check and the sum wrap around past 2^32-1. The port adds with checked_add in the check and the sum, and rejects the image when either overflows. For example, a DNG whose IFD0 holds a 16×12 RGB thumbnail at offset 206, with only IFD0's StripOffsets and StripByteCounts changed:

C++ Rust
StripOffsets 206, 205; StripByteCounts 4,294,967,295, 5 (206 + 4,294,967,295 wraps to 205; the counts add up to 4) thumbnail 16×12, 4 bytes at 206 no thumbnail
StripOffsets 0, 4,294,967,295; StripByteCounts 4,294,967,295, 1 (the counts add up to 0) thumbnail 16×12, 0 bytes at 0 no thumbnail

With one strip (StripOffsets 206, StripByteCounts 576), or two strips that do not wrap (206, 506; 300, 276), both return the 16×12 thumbnail, 576 bytes at 206.

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, when a strip image's offsets and byte counts pass 2^32-1 when added, should GetImageData add them as uint32_t and keep the image (like the C++ library), or reject the image (like the Rust port)?

This PR implements C++'s behaviour.

Fix

offsets_are_consecutive and get_image_data add with wrapping_add, as C++'s uint32_t additions do:

-            length = strip_byte_counts
-                .iter()
-                .try_fold(0u32, |acc, &count| acc.checked_add(count))
-                .ok_or(TiffError::CorruptedEntry {
-                    offset: *strip_offsets.first().unwrap_or(&0),
-                })?;
+            length = strip_byte_counts.iter().fold(0u32, |acc, &count| acc.wrapping_add(count));
...
-            .all(|(w, &count)| w[0].checked_add(count) == Some(w[1]))
+            .all(|(w, &count)| w[0].wrapping_add(count) == w[1])

Replayed before and after:

C++ Rust main Rust patched
206, 205 / 4,294,967,295, 5 16×12, 4 bytes at 206 none 16×12, 4 bytes at 206
0, 4,294,967,295 / 4,294,967,295, 1 16×12, 0 bytes at 0 none 16×12, 0 bytes at 0
one strip, and two strips that do not wrap 16×12, 576 bytes at 206 same same

Notes for review:

  • The image C++ keeps here has a wrapped length: 4 or 0 bytes for a 16×12 RGB thumbnail. If rejecting it is the better behavior, we can close this PR and keep the port's checked_add.
  • Every TIFF-based format goes through GetImageData: DNG, NEF, ORF, PEF and the rest. The port's tests pass unchanged (44 + 174).

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.

1 participant