bcpack: the destination guard, the dictionary's reach, and the degenerate streams #134

Open
xiaotu030202 wants to merge 1 commit from upstream-pr/134 into main
xiaotu030202 commented 2026-11-25 14:11:01 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by xiaotu030202 on 2026-11-25T13:26:57Z.


Three properties that hold, and nothing held them

decode refuses a destination that is not whole blocks

if dst.is_empty() || !dst.len().is_multiple_of(block) {
    return Err(Error("destination is not a whole number of blocks"));
}

The write below uses chunks_exact_mut, which silently drops a partial trailing block. Without that guard, a dst one byte short of a block comes back Ok(()) with the last byte untouched — a caller reads stale data and gets no error.

Checked by mutation:

drop `!dst.len().is_multiple_of(block)`  ->  FAILED
  "bc1 with 1 bytes (0 blocks of 8): Ok(())"

That string is the guard's whole justification.

The test walks all six BC numbers at 0, 1, 2 and 5 blocks, each one byte short and one byte long, and asserts the message. It also pins the other two refusals: an empty destination, and a BC number outside 1/3–7.

pick indexes the dictionary with a byte, and the size is exact

r => &dictionary[r - i - 1],

r comes from references, which returns usize::from(u8) — so at most 255. At block 0 the deepest reachable index is 255 - 1 = 254. DICTIONARY is 255, which is exactly enough and no more.

There is no .get(), no clamp: the bound is arithmetic, not defensive. Shrinking the constant by one panics on the first block that reaches back that far, and nothing said so.

DICTIONARY = 254  ->  FAILED
  "a reference of 255 at block 0 wants dictionary[254], but DICTIONARY is 254"

The degenerate streams

noise_never_panics walks pseudo-random streams of many lengths. The shapes a real chunk actually starts from — all zeros, all ones, 0xAA, 0x55 — were not among them.

the_degenerate_streams_never_panic runs four fills × nine lengths × six BC numbers × two block counts. It asserts only "no panic", which is all a decoder can promise for input it was not given a size for.

Verification

Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. No game files, no GPU.

$ cargo test -p victory-formats
test result: ok. 37 passed; 0 failed; 4 ignored     (34 before this)

$ cargo clippy -p victory-formats --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok

No bug found

All three properties hold on the code as written. I went looking for an out-of-bounds in pick and a silent short write in decode; the first is guarded by DICTIONARY == 255 matching the byte range exactly, the second by the guard now under test. Both are on record so a change to either side has to notice the other.

One thing worth flagging for whoever owns this file: the pick bound is arithmetically exact rather than defensive. If the reference encoding ever widens past a byte, that is a panic rather than an error — and the_dictionary_covers_every_reference_a_record_can_name is what would say so first.

Where this sits among my other PRs on this crate

PR file what
#123 texture.rs the DXGI format table
#125 texture.rs addressable() wrong for multisampled linear layouts (a bug)
#129 texture.rs walk staying inside its buffers
#131 texture.rs chain length and the stencil plane's block
this bcpack.rs the destination guard, the dictionary's reach, degenerate streams

Five PRs in one crate is a lot; happy to squash them into two (the texture.rs bugfix, and the tests) if that is easier to review.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6605545345 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/134). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T13:26:57Z. --- ## Three properties that hold, and nothing held them ### `decode` refuses a destination that is not whole blocks ```rust if dst.is_empty() || !dst.len().is_multiple_of(block) { return Err(Error("destination is not a whole number of blocks")); } ``` The write below uses `chunks_exact_mut`, which **silently drops a partial trailing block**. Without that guard, a `dst` one byte short of a block comes back `Ok(())` with the last byte untouched — a caller reads stale data and gets no error. **Checked by mutation:** ``` drop `!dst.len().is_multiple_of(block)` -> FAILED "bc1 with 1 bytes (0 blocks of 8): Ok(())" ``` That string is the guard's whole justification. The test walks all six BC numbers at 0, 1, 2 and 5 blocks, each **one byte short and one byte long**, and asserts the message. It also pins the other two refusals: an empty destination, and a BC number outside 1/3–7. ### `pick` indexes the dictionary with a byte, and the size is exact ```rust r => &dictionary[r - i - 1], ``` `r` comes from `references`, which returns `usize::from(u8)` — so at most 255. At block 0 the deepest reachable index is `255 - 1 = 254`. `DICTIONARY` is `255`, which is **exactly enough and no more**. There is no `.get()`, no clamp: the bound is arithmetic, not defensive. Shrinking the constant by one panics on the first block that reaches back that far, and nothing said so. ``` DICTIONARY = 254 -> FAILED "a reference of 255 at block 0 wants dictionary[254], but DICTIONARY is 254" ``` ### The degenerate streams `noise_never_panics` walks pseudo-random streams of many lengths. The shapes a real chunk actually starts from — all zeros, all ones, `0xAA`, `0x55` — were not among them. `the_degenerate_streams_never_panic` runs four fills × nine lengths × six BC numbers × two block counts. It asserts only "no panic", which is all a decoder can promise for input it was not given a size for. ## Verification Windows 11, Rust 1.98.1 (pinned), MSVC 14.44.35207. **No game files, no GPU.** ``` $ cargo test -p victory-formats test result: ok. 37 passed; 0 failed; 4 ignored (34 before this) $ cargo clippy -p victory-formats --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## No bug found All three properties hold on the code as written. I went looking for an out-of-bounds in `pick` and a silent short write in `decode`; the first is guarded by `DICTIONARY == 255` matching the byte range exactly, the second by the guard now under test. Both are on record so a change to either side has to notice the other. **One thing worth flagging for whoever owns this file:** the `pick` bound is arithmetically exact rather than defensive. If the reference encoding ever widens past a byte, that is a panic rather than an error — and `the_dictionary_covers_every_reference_a_record_can_name` is what would say so first. ## Where this sits among my other PRs on this crate | PR | file | what | |---|---|---| | #123 | `texture.rs` | the DXGI format table | | #125 | `texture.rs` | `addressable()` wrong for multisampled linear layouts (**a bug**) | | #129 | `texture.rs` | `walk` staying inside its buffers | | #131 | `texture.rs` | chain length and the stencil plane's block | | this | `bcpack.rs` | the destination guard, the dictionary's reach, degenerate streams | Five PRs in one crate is a lot; happy to squash them into two (the `texture.rs` bugfix, and the tests) if that is easier to review.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/134:upstream-pr/134'
git switch 'upstream-pr/134'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/134'
git switch 'upstream-pr/134'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/134'
git switch 'upstream-pr/134'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/134'
git switch 'main'
git merge --squash 'upstream-pr/134'
git switch 'main'
git merge --ff-only 'upstream-pr/134'
git switch 'main'
git merge 'upstream-pr/134'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Zettrox/ztrx!134
No description provided.