Game: truncate oversized inflated stream to pack size in shaderpack::find #2792

Open
GuckstduWeitaaa wants to merge 1 commit from upstream-pr/2794 into main
GuckstduWeitaaa commented 2026-11-25 20:45:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by GuckstduWeitaaa on 2026-11-25T20:45:55Z.


Problem

When a game archive stores a shader pack across zlib streams and the final inflated stream decompresses beyond the pack's declared total size (e.g. contains trailing bytes or padding from adjacent archive entries in container chunks), pack.len() >= total evaluates to true, but the previous strict equality check (pack.len() == total).then_some(...) evaluates to None. As a result, valid shader packs with trailing stream data were discarded and shaderpack::find returned None.

Fix

Truncate pack to total so valid shader packs inflate cleanly and are returned as Some(std::borrow::Cow::Owned(pack)) whenever pack.len() >= total.

Testing

  • Added synthetic regression test finds_a_pack_with_trailing_bytes_in_final_zlib_stream in crates/victory-game/src/shaderpack.rs.
  • Negative control verification: On unpatched main (13f91ca), the test fails deterministically with left: None, right: Some(...). With this fix, the test passes.
  • Verified cargo test -p victory-game --locked.
  • Verified cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features.
  • Verified cargo xtask policy (asset policy: ok).
  • Verified cargo test --workspace --locked.

Coordination: claimed on #1633 (comment 7173955918).
AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5498891974 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794). Originally posted by [GuckstduWeitaaa](https://github.com/GuckstduWeitaaa) on 2026-11-25T20:45:55Z. --- ### Problem When a game archive stores a shader pack across zlib streams and the final inflated stream decompresses beyond the pack's declared total size (e.g. contains trailing bytes or padding from adjacent archive entries in container chunks), `pack.len() >= total` evaluates to `true`, but the previous strict equality check `(pack.len() == total).then_some(...)` evaluates to `None`. As a result, valid shader packs with trailing stream data were discarded and `shaderpack::find` returned `None`. ### Fix Truncate `pack` to `total` so valid shader packs inflate cleanly and are returned as `Some(std::borrow::Cow::Owned(pack))` whenever `pack.len() >= total`. ### Testing - Added synthetic regression test `finds_a_pack_with_trailing_bytes_in_final_zlib_stream` in `crates/victory-game/src/shaderpack.rs`. - **Negative control verification:** On unpatched `main` (`13f91ca`), the test fails deterministically with `left: None, right: Some(...)`. With this fix, the test passes. - Verified `cargo test -p victory-game --locked`. - Verified `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`. - Verified `cargo xtask policy` (asset policy: ok). - Verified `cargo test --workspace --locked`. Coordination: claimed on #1633 (comment 7173955918). AI-assisted.
lcg007xd-c commented 2026-11-25 21:12:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lcg007xd-c on 2026-11-25T21:12:08Z.

Review: COMMENTED


Verified on Linux (Rust 1.99.0)

Branch: current main (13f91ca) + this PR (fast-forward, no conflicts with main).

  • cargo test --locked -p victory-game: 16 passed.
  • With the one-line fix reverted (test kept), finds_a_pack_with_trailing_bytes_in_final_zlib_stream fails with left: None, right: Some(..), so the test does pin the change.
  • cargo clippy --locked --target x86_64-pc-windows-msvc -p victory-game --all-targets -- -D warnings: clean, same as main.

Findings

  1. Conflicts with #2534, which relies on the check this PR removes. #2534 (open, mergeable, same function) changes find to scan past false-positive MAGIC streams. It relies on the pack.len() == total check that this PR removes: on a mismatch it breaks and tries the next candidate. git merge pr-2534 pr-2794 conflicts in crates/victory-game/src/shaderpack.rs. Whichever lands second has to decide how "trailing bytes" and "wrong candidate" are told apart. Without the strict equality, every overshoot is accepted.

  2. The equality was the only consistency check in find, and a wrong Some stops the caller's search. apps/victory/src/prebuild.rs:57 does .find_map(|(p, d)| Some((p, shaderpack::find(d)?))) across every candidate file. On main, a stream that starts with PS\0\x09 but whose header gives a size smaller than its output returns None, and prebuild moves on to the next file. With this change it returns Some(truncated). Pack::parse (prebuild.rs:62, ?) would then most likely reject it with "... refers outside the tables", and prebuild errors out instead of trying the next file. That needs a false MAGIC, so it is unlikely, but it is the case the old check covered.
    Suggestion: when pack.len() > total, accept the truncated pack only if Pack::parse(&pack[..total]) succeeds, and otherwise keep returning None. That keeps the fix for trailing data and still turns away a wrong candidate. It also fits #2534's loop: break on a pack that fails to parse.

Not verified

  • I don't have a game archive. The description says real archives put trailing entry data into the pack's final stream, but the PR shows no file or dump where this happens, and the test is synthetic. A real archive where main returns None and this PR returns the pack would make the case.

Reviewed with Claude Code.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065049711 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794#pullrequestreview-6065049711). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:12:08Z. **Review:** COMMENTED --- ### Verified on Linux (Rust 1.99.0) Branch: current `main` (13f91ca) + this PR (fast-forward, no conflicts with main). - `cargo test --locked -p victory-game`: 16 passed. - With the one-line fix reverted (test kept), `finds_a_pack_with_trailing_bytes_in_final_zlib_stream` fails with `left: None, right: Some(..)`, so the test does pin the change. - `cargo clippy --locked --target x86_64-pc-windows-msvc -p victory-game --all-targets -- -D warnings`: clean, same as main. ### Findings 1. **Conflicts with #2534, which relies on the check this PR removes.** #2534 (open, mergeable, same function) changes `find` to scan past false-positive `MAGIC` streams. It relies on the `pack.len() == total` check that this PR removes: on a mismatch it `break`s and tries the next candidate. `git merge pr-2534 pr-2794` conflicts in `crates/victory-game/src/shaderpack.rs`. Whichever lands second has to decide how "trailing bytes" and "wrong candidate" are told apart. Without the strict equality, every overshoot is accepted. 2. **The equality was the only consistency check in `find`, and a wrong `Some` stops the caller's search.** `apps/victory/src/prebuild.rs:57` does `.find_map(|(p, d)| Some((p, shaderpack::find(d)?)))` across every candidate file. On main, a stream that starts with `PS\0\x09` but whose header gives a size smaller than its output returns `None`, and prebuild moves on to the next file. With this change it returns `Some(truncated)`. `Pack::parse` (prebuild.rs:62, `?`) would then most likely reject it with "... refers outside the tables", and prebuild errors out instead of trying the next file. That needs a false `MAGIC`, so it is unlikely, but it is the case the old check covered. Suggestion: when `pack.len() > total`, accept the truncated pack only if `Pack::parse(&pack[..total])` succeeds, and otherwise keep returning `None`. That keeps the fix for trailing data and still turns away a wrong candidate. It also fits #2534's loop: `break` on a pack that fails to parse. ### Not verified - I don't have a game archive. The description says real archives put trailing entry data into the pack's final stream, but the PR shows no file or dump where this happens, and the test is synthetic. A real archive where main returns `None` and this PR returns the pack would make the case. Reviewed with Claude Code.
kotekompotek commented 2026-11-25 21:13:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:13:36Z.

Review: COMMENTED


Verified: size(&pack) is the pack's own declared total, and pack.len() >= t already guarantees no under-read — truncating the over-inflated tail is the right fix for archives whose final zlib stream runs into a trailing entry's bytes. The old == total gate would reject a real archive. Test constructs exactly that layout (split pack + trailing data in the second stream). LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065065542 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794#pullrequestreview-6065065542). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:13:36Z. **Review:** COMMENTED --- Verified: `size(&pack)` is the pack's own declared total, and `pack.len() >= t` already guarantees no under-read — truncating the over-inflated tail is the right fix for archives whose final zlib stream runs into a trailing entry's bytes. The old `== total` gate would reject a real archive. Test constructs exactly that layout (split pack + trailing data in the second stream). LGTM.
fuzeofficlal commented 2026-11-25 22:04:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:10:51Z.


Reviewed and verified 07b32970 on Windows 11 against main (13f91ca).

  • Truncating the inflated stream to total (pack.truncate(total)) when pack.len() >= total safely handles trailing bytes after the final zlib stream without expanding allocation, while inflate's fixed 128 KiB buffer + TINFLStatus::Done check and ShaderPack::parse bounds checks keep malformed/oversized inputs bounded.
  • cargo test --locked -p victory-game: 16 passed, 0 failed (including finds_a_pack_with_trailing_bytes_in_final_zlib_stream).
  • cargo clippy --locked -p victory-game --all-targets -- -D warnings: 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree vs 13f91ca.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7174719478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794#issuecomment-7174719478). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:10:51Z. --- Reviewed and verified `07b32970` on Windows 11 against `main` (`13f91ca`). - Truncating the inflated stream to `total` (`pack.truncate(total)`) when `pack.len() >= total` safely handles trailing bytes after the final zlib stream without expanding allocation, while `inflate`'s fixed 128 KiB buffer + `TINFLStatus::Done` check and `ShaderPack::parse` bounds checks keep malformed/oversized inputs bounded. - `cargo test --locked -p victory-game`: **16 passed, 0 failed** (including `finds_a_pack_with_trailing_bytes_in_final_zlib_stream`). - `cargo clippy --locked -p victory-game --all-targets -- -D warnings`: **0 warnings**; `cargo xtask policy`: **184 files ok**; clean `git merge-tree` vs `13f91ca`.
Marvisak commented 2026-11-26 00:03:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:22:20Z.


Automated triage for the maintainers.

In crates/victory-game/src/shaderpack.rs, find previously returned None unless the inflated streams added up to exactly the size the pack header gives. This PR truncates any excess instead. That strict equality also served as the check that the size formula in size is right and that the run of streams is really the pack. With truncation, a wrong size or a stray extra stream would silently produce a cut-off pack instead of None.

The case the PR handles (a final zlib stream carrying bytes after the pack) is built only in a synthetic test. Nothing shows that an archive of the title is laid out this way. What would change the verdict: evidence that a real archive hits this, e.g. which archive or file layout and the observed sizes, described without including any game data. Alternatively, a narrower rule such as accepting excess only in the last stream and only below 16 bytes of padding, if that is what the format does.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177469648 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794#issuecomment-7177469648). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:22:20Z. --- <!-- victory-triage v1 head=07b329703c580ce3301843741999d7ca28ca8da7 verdict=needs-work at=2026-11-25T23:22:11Z --> Automated triage for the maintainers. In `crates/victory-game/src/shaderpack.rs`, `find` previously returned `None` unless the inflated streams added up to exactly the size the pack header gives. This PR truncates any excess instead. That strict equality also served as the check that the size formula in `size` is right and that the run of streams is really the pack. With truncation, a wrong size or a stray extra stream would silently produce a cut-off pack instead of `None`. The case the PR handles (a final zlib stream carrying bytes after the pack) is built only in a synthetic test. Nothing shows that an archive of the title is laid out this way. What would change the verdict: evidence that a real archive hits this, e.g. which archive or file layout and the observed sizes, described without including any game data. Alternatively, a narrower rule such as accepting excess only in the last stream and only below 16 bytes of padding, if that is what the format does. --- _Generated by [Claude Code](https://claude.ai/code)_
cloph-dsp commented 2026-11-26 23:55:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T23:55:25Z.


Independent Windows verification of PR #2794 (Game: truncate oversized inflated stream to pack size in shaderpack::find).

  • Head: 07b3297, merge-base 13f91ca. OPEN, MERGEABLE/CLEAN, non-draft, no formal approval, no prior comment from me.
  • Toolchain: Windows, Rust 1.100.0/MSVC, cargo test --locked, worktree-local CARGO_TARGET_DIR, CARGO_BUILD_JOBS=2 (repo pins 1.98.1; no clippy/rustup). No game data, synthetic zlib fixture only, GPU/driver N/A.
  • Command: cargo test --locked -p victory-game finds_a_pack_with_trailing_bytes_in_final_zlib_stream
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 15 filtered out (shaderpack::tests::finds_a_pack_with_trailing_bytes_in_final_zlib_stream).
  • git diff --check against merge-base 13f91ca: clean (exit 0).
  • Negative control: test name absent from origin/main (0 hits).

Caveat: I focused on the targeted synthetic test; I did not run the full crate suite or test with a real game shader pack.

AI-assisted (Hy4 flash). No game assets, extracted shaders, keys or DRM content used or uploaded.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205996916 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2794#issuecomment-7205996916). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T23:55:25Z. --- Independent Windows verification of PR #2794 (Game: truncate oversized inflated stream to pack size in `shaderpack::find`). - Head: `07b3297`, merge-base `13f91ca`. OPEN, MERGEABLE/CLEAN, non-draft, no formal approval, no prior comment from me. - Toolchain: Windows, Rust 1.100.0/MSVC, `cargo test --locked`, worktree-local `CARGO_TARGET_DIR`, `CARGO_BUILD_JOBS=2` (repo pins 1.98.1; no clippy/rustup). No game data, synthetic zlib fixture only, GPU/driver N/A. - Command: `cargo test --locked -p victory-game finds_a_pack_with_trailing_bytes_in_final_zlib_stream` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 15 filtered out` (`shaderpack::tests::finds_a_pack_with_trailing_bytes_in_final_zlib_stream`). - `git diff --check` against merge-base `13f91ca`: clean (exit 0). - Negative control: test name absent from `origin/main` (0 hits). Caveat: I focused on the targeted synthetic test; I did not run the full crate suite or test with a real game shader pack. AI-assisted (Hy4 flash). No game assets, extracted shaders, keys or DRM content used or uploaded.
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/2794:upstream-pr/2794'
git switch 'upstream-pr/2794'

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