formats: every mip writes inside the buffers it was given #129

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

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


What this adds

walk checks tiled_len >= size once, then computes each row's offset as base + row(...) and hands tiled_row + tiled to the copy closure, which indexes with [..]:

let client = |tiled_row: usize, linear_row: usize, len: usize| {
    linear[l..][..len].copy_from_slice(&tiled[t..][..len]);   // panic if past the end
};

Nothing bounds those per row. A mip chain whose tail is placed wrong — the tail is where first_tail_mip puts the small mips, and where padded_dim's rounding differs from dim's — would index past the buffer and panic rather than answer TooSmall. No test walked a chain at its exact size, so nothing would have caught it.

The two tests

every_mip_writes_inside_the_buffers_it_was_given — the seven addressable swizzles, each at four shapes:

shape what it exercises
64×64, full mip chain a long chain, many tails
64×64, 3 mips a chain that stops early
128×32, 2 mips non-square, so padded_dim's rounding differs per axis
16×16, 4 mips a chain that reaches 1×1

For each, every mip of every chain is detiled into a tiled buffer of exactly size and a linear buffer of exactly the rows. If any mip's tail offset landed past the chain the indexing would panic here.

a_short_tiled_buffer_is_refused_before_any_row_is_read — the check walk does make: one byte short is TooSmall, not an index. Covers LINEAR and a tiled mode.

Checked by mutation

drop `tiled_len < self.size` from walk   ->  FAILED (panicked at the slice index)

Without the check the second test panics where the first would have, so both are load-bearing.

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. 32 passed; 0 failed; 4 ignored     (30 on #125 alone, 24 before either)

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

$ cargo xtask policy
asset policy: 172 files ok

What I did not find

No bug here. I went looking for one — the row offsets are computed without a per-row bound, and I expected a tail mip to land past size for some shape. It does not: all 28 mip-and-swizzle combinations are inside. The bounds are right; they were just unheld at the exact size.

The (depth as usize - 1) and (m.height as usize - 1) in need looked like an underflow risk too. They are not: dim() ends in .max(1), so every mip is at least 1×1×1.

Scope

Same file, same region, and orthogonal to #125's one-line fix — that one is about addressable() answering wrongly for multisampled linear layouts; this is about walk staying inside its buffers. Kept separate so either can be reviewed on its own.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6605359414 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/129). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T13:16:50Z. --- ## What this adds `walk` checks `tiled_len >= size` **once**, then computes each row's offset as `base + row(...)` and hands `tiled_row + tiled` to the copy closure, which indexes with `[..]`: ```rust let client = |tiled_row: usize, linear_row: usize, len: usize| { linear[l..][..len].copy_from_slice(&tiled[t..][..len]); // panic if past the end }; ``` Nothing bounds those per row. A mip chain whose tail is placed wrong — the tail is where `first_tail_mip` puts the small mips, and where `padded_dim`'s rounding differs from `dim`'s — would index past the buffer and **panic** rather than answer `TooSmall`. No test walked a chain at its exact size, so nothing would have caught it. ## The two tests **`every_mip_writes_inside_the_buffers_it_was_given`** — the seven addressable swizzles, each at four shapes: | shape | what it exercises | |---|---| | 64×64, full mip chain | a long chain, many tails | | 64×64, 3 mips | a chain that stops early | | 128×32, 2 mips | non-square, so `padded_dim`'s rounding differs per axis | | 16×16, 4 mips | a chain that reaches 1×1 | For each, every mip of every chain is detiled into a tiled buffer of exactly `size` and a linear buffer of exactly the rows. If any mip's tail offset landed past the chain the indexing would panic here. **`a_short_tiled_buffer_is_refused_before_any_row_is_read`** — the check `walk` *does* make: one byte short is `TooSmall`, not an index. Covers `LINEAR` and a tiled mode. ## Checked by mutation ``` drop `tiled_len < self.size` from walk -> FAILED (panicked at the slice index) ``` Without the check the second test panics where the first would have, so both are load-bearing. ## 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. 32 passed; 0 failed; 4 ignored (30 on #125 alone, 24 before either) $ cargo clippy -p victory-formats --all-targets clean $ cargo xtask policy asset policy: 172 files ok ``` ## What I did not find **No bug here.** I went looking for one — the row offsets are computed without a per-row bound, and I expected a tail mip to land past `size` for some shape. It does not: all 28 mip-and-swizzle combinations are inside. The bounds are right; they were just unheld at the exact size. The `(depth as usize - 1)` and `(m.height as usize - 1)` in `need` looked like an underflow risk too. They are not: `dim()` ends in `.max(1)`, so every mip is at least 1×1×1. ## Scope Same file, same region, and orthogonal to #125's one-line fix — that one is about `addressable()` answering wrongly for multisampled linear layouts; this is about `walk` staying inside its buffers. Kept separate so either can be reviewed on its own.
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/129:upstream-pr/129'
git switch 'upstream-pr/129'

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/129'
git switch 'upstream-pr/129'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/129'
git switch 'upstream-pr/129'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/129'
git switch 'main'
git merge --squash 'upstream-pr/129'
git switch 'main'
git merge --ff-only 'upstream-pr/129'
git switch 'main'
git merge 'upstream-pr/129'
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!129
No description provided.