Skip to content

Keep a CR3 box's size and end in usize on 32-bit targets, as GetNextBox does - #37

Open
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:cr3-box-size-in-size-t
Open

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:cr3-box-size-in-size-t

Conversation

@kunpai

@kunpai kunpai commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Keep a CR3 box's size and end in usize on 32-bit targets, as GetNextBox does

Problem

GetNextBox (C++, Rust) reads a CR3 box's header: a 32-bit size, a tag, and a 64-bit size after the tag when the 32-bit size is 1. C++ holds the size in a size_t and checks the box's end against the largest size_t. The port holds the size and the end in a u64. On a 64-bit target, the two agree. On a 32-bit target, where size_t is 32 bits, C++ differs in two ways:

  1. A 64-bit size of 2^32 or more: C++ keeps only its low 32 bits, and rejects the box when they are below its 16-byte header.
  2. A box that ends past 2^32-1: C++ rejects the box.

The port reads both boxes on either target.

For example, a CR3 from the campaigns where the box right after stsd's header, where ProcessStblBox looks for CRAW, is not CRAW and has a 32-bit size of 1 and a 64-bit size of 0x5700000000000000. ProcessStblBox skips a box there that is not CRAW, but fails on one that is not a valid box. On x86-64, C++ keeps the whole size, skips the box and returns the 1620×1080 preview from the PRVW box. On 32-bit ARM, the low 32 bits are 0, so C++ rejects the box and GetPreviewImageData returns kFail. With the box's 32-bit size set to 0xFFFFFF00 instead, the box (at byte 400) ends at byte 4,294,967,440, and C++ on 32-bit ARM rejects it too:

# C++ x86-64 C++ 32-bit ARM Rust x86-64 and 32-bit ARM
1 64-bit size 0x5700000000000000 kOk, 1620×1080 preview kFail, no preview kOk, 1620×1080 preview
2 32-bit size 0xFFFFFF00 kOk, 1620×1080 preview kFail, no preview kOk, 1620×1080 preview

With a 64-bit size whose low 32 bits are 32, or a 32-bit size of 0xFFFFFE00 (which ends before 2^32), both targets give the preview.

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, on a 32-bit target:

  1. A 64-bit box size of 2^32 or more: should GetNextBox keep only the size's low 32 bits (like the C++ library), or the whole size (like the Rust port)?
  2. A box that ends past 2^32-1: should GetNextBox reject the box (like the C++ library), or read it (like the Rust port)?

This PR implements C++'s behaviour.

Fix

In read_next_box, the port now keeps the size and the box's end in usize, as C++ keeps them in size_t:

-        let length_64 = u64::from_be_bytes(len_buf);
+        let length_64 = u64::from_be_bytes(len_buf) as usize as u64;
-    let next_box_offset = match offset.checked_add(total_length) {
-        Some(val) => val,
+    let next_box_offset = match offset_usize.checked_add(total_length as usize) {
+        Some(val) => val as u64,

The examples above then match C++. Replayed on 32-bit ARM, before and after:

# C++ Rust main Rust patched
1 64-bit size 0x5700000000000000 kFail, no preview kOk, 1620×1080 preview kFail, no preview
2 32-bit size 0xFFFFFF00 kFail, no preview kOk, 1620×1080 preview kFail, no preview

Notes for review:

  • Only 32-bit targets change: on a 64-bit target, both casts keep every value, so both examples give the preview before and after, and the port's tests pass (44 + 174). On 32-bit ARM, the same 44 + 172 tests pass before and after. test_read_next_box_clean_eof_handling and test_piex_stream_t_layout fail there on master already, and the same way with this PR.
  • C++'s answer seems like a bug: the sizes in the file are 64-bit, and C++ reads them whole on a 64-bit target. To keep the port's behavior, we can close this PR. C++ would then need to keep a box's size and end in 64 bits, as the port does.

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