Game: use checked length arithmetic and avoid discarded Vec allocations in sgd5 #2575

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2577 into main
reeledMarek commented 2026-11-25 16:08:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T16:08:41Z.


Summary

Fixes unchecked arithmetic and wasted allocations in victory-game/src/sgd5.rs:

  1. Replaces unchecked length/offset arithmetic with checked_mul / checked_add so malformed SGD5 payloads return a clean parse error instead of overflowing or panicking.
  2. Removes discarded intermediate Vec allocations during SGD5 entry decoding, parsing directly from borrowed slices (as_chunks::<4>()).

Testing

  • cargo test -p victory-game --locked (16/16 passed)
  • cargo clippy -p victory-game --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings (clean)
  • cargo test -p xtask --locked (6/6 passed)
  • Synthetic unit tests only; no game files used.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6608587461 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2577). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T16:08:41Z. --- ### Summary Fixes unchecked arithmetic and wasted allocations in `victory-game/src/sgd5.rs`: 1. Replaces unchecked length/offset arithmetic with `checked_mul` / `checked_add` so malformed SGD5 payloads return a clean parse error instead of overflowing or panicking. 2. Removes discarded intermediate `Vec` allocations during SGD5 entry decoding, parsing directly from borrowed slices (`as_chunks::<4>()`). ### Testing - `cargo test -p victory-game --locked` (16/16 passed) - `cargo clippy -p victory-game --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings` (clean) - `cargo test -p xtask --locked` (6/6 passed) - Synthetic unit tests only; no game files used.
lcg007xd-c commented 2026-11-25 22:03:48 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Verified on Linux (Rust 1.99.0)

Branch: current main (13f91ca) + this PR (merges cleanly; the PR is one commit behind main, and it also merges cleanly with #2575).

  • cargo test --locked -p victory-game: 16 passed.
  • I restored sgd5.rs to main and kept only the new record_rejects_overflowing_length test. It still passes (all 5 sgd5 tests pass). So the test doesn't show anything this PR changes.
  • cargo clippy --locked --target x86_64-pc-windows-msvc -p victory-game --all-targets -- -D warnings: clean, same as main.

Findings

  1. The regression test passes without the fix. On a 64-bit target, at + n with n = u32::MAX as usize and a small at doesn't overflow usize. On main, file.get(at + 12..at + n)? simply returns None, and records comes back empty, which is exactly what the test asserts. Either drop the test or reword it as a coverage test for an oversized length (which is still worth keeping). It shouldn't be presented as a regression test for an overflow.

  2. None of the checked arithmetic is reachable on the targets VIctory builds. VIctory builds x86-64 only (Windows MSVC, plus the Linux crates).

    • In record (sgd5.rs:147-168), every operand is at < file.len() plus a u32, which can't wrap a 64-bit usize.
    • In effect, every count that feeds loaded has already been bounded by Cursor::bytes, which uses checked_add and file.get (sgd5.rs:188). So s.len(), techniques.len(), passes.len() and extra are at most the file size. blocks is masked to 24 bits, and each remaining term is a single u32.

    The checked_* versions are harmless, but the description's "malformed SGD5 payloads return a clean parse error instead of overflowing or panicking" describes a failure that can't happen on this codebase's targets. I'd reword it as defensive hardening (or 32-bit portability), so the maintainer isn't looking for a crash that was fixed.

  3. The allocation changes are fine. skip_items keeps the checked_mul bound of items. u32s via as_chunks::<4>() has no remainder, because exactly n * 4 bytes are taken. extra is only ever used as a count. No behaviour change, and it saves two discarded Vecs per effect.

Not verified

  • Parsing real SGD5 databases (no game files). The refactor is behaviour-preserving by reading, and the synthetic tests pass.

Reviewed with Claude Code.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065178551 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2577#pullrequestreview-6065178551). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:23:59Z. **Review:** COMMENTED --- ### Verified on Linux (Rust 1.99.0) Branch: current `main` (13f91ca) + this PR (merges cleanly; the PR is one commit behind main, and it also merges cleanly with #2575). - `cargo test --locked -p victory-game`: 16 passed. - I restored `sgd5.rs` to main and kept only the new `record_rejects_overflowing_length` test. It **still passes** (all 5 `sgd5` tests pass). So the test doesn't show anything this PR changes. - `cargo clippy --locked --target x86_64-pc-windows-msvc -p victory-game --all-targets -- -D warnings`: clean, same as main. ### Findings 1. **The regression test passes without the fix.** On a 64-bit target, `at + n` with `n = u32::MAX as usize` and a small `at` doesn't overflow `usize`. On main, `file.get(at + 12..at + n)?` simply returns `None`, and `records` comes back empty, which is exactly what the test asserts. Either drop the test or reword it as a coverage test for an oversized length (which is still worth keeping). It shouldn't be presented as a regression test for an overflow. 2. **None of the checked arithmetic is reachable on the targets VIctory builds.** VIctory builds x86-64 only (Windows MSVC, plus the Linux crates). - In `record` (sgd5.rs:147-168), every operand is `at < file.len()` plus a `u32`, which can't wrap a 64-bit `usize`. - In `effect`, every count that feeds `loaded` has already been bounded by `Cursor::bytes`, which uses `checked_add` and `file.get` (sgd5.rs:188). So `s.len()`, `techniques.len()`, `passes.len()` and `extra` are at most the file size. `blocks` is masked to 24 bits, and each remaining term is a single `u32`. The `checked_*` versions are harmless, but the description's "malformed SGD5 payloads return a clean parse error instead of overflowing or panicking" describes a failure that can't happen on this codebase's targets. I'd reword it as defensive hardening (or 32-bit portability), so the maintainer isn't looking for a crash that was fixed. 3. The allocation changes are fine. `skip_items` keeps the `checked_mul` bound of `items`. `u32s` via `as_chunks::<4>()` has no remainder, because exactly `n * 4` bytes are taken. `extra` is only ever used as a count. No behaviour change, and it saves two discarded `Vec`s per effect. ### Not verified - Parsing real SGD5 databases (no game files). The refactor is behaviour-preserving by reading, and the synthetic tests pass. Reviewed with Claude Code.
Marvisak commented 2026-11-25 23:06:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:17:41Z.


Automated triage for the maintainers.

Proposed close: guards for cases that cannot happen. VIctory builds only for x86_64-pc-windows-msvc (rust-toolchain.toml). In sgd5::record and sgd5::effect, every term added is either a u32 widened to a 64-bit usize or the length of a slice already read from the file. at + 12, at + n and the loaded sum cannot overflow there. On main an oversized length already makes file.get(..) / Cursor::bytes return None. The new test record_rejects_overflowing_length passes on main unchanged for that reason, so it does not show a bug. What remains is skip_items in place of two discarded small Vecs in an offline parser, which does not justify the change by itself.

This PR will be closed in 48 hours unless the author pushes a change (for example a case that panics or misparses on main) or a maintainer removes the triage: proposed close label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176181905 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2577#issuecomment-7176181905). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:17:41Z. --- <!-- victory-triage v1 head=1e418d296b8b6800fa55f7d67aba0632714d8fc8 verdict=proposed-close at=2026-11-25T22:17:30Z --> Automated triage for the maintainers. **Proposed close: guards for cases that cannot happen.** VIctory builds only for `x86_64-pc-windows-msvc` (`rust-toolchain.toml`). In `sgd5::record` and `sgd5::effect`, every term added is either a `u32` widened to a 64-bit `usize` or the length of a slice already read from the file. `at + 12`, `at + n` and the `loaded` sum cannot overflow there. On `main` an oversized length already makes `file.get(..)` / `Cursor::bytes` return `None`. The new test `record_rejects_overflowing_length` passes on `main` unchanged for that reason, so it does not show a bug. What remains is `skip_items` in place of two discarded small `Vec`s in an offline parser, which does not justify the change by itself. This PR will be closed in 48 hours unless the author pushes a change (for example a case that panics or misparses on `main`) or a maintainer removes the `triage: proposed close` label. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-26 06:02:31 +00:00
reeledMarek commented 2026-11-26 06:03:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T05:43:32Z.


Closing per triage and @lcg007xd-c's review: on x86-64 Windows the usize additions in sgd5 cannot wrap and file.get/Cursor::bytes already reject oversized lengths, so the remaining skip_items refactor alone does not justify a PR. Thanks for the thorough check!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183748539 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2577#issuecomment-7183748539). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T05:43:32Z. --- Closing per triage and @lcg007xd-c's review: on x86-64 Windows the `usize` additions in `sgd5` cannot wrap and `file.get`/`Cursor::bytes` already reject oversized lengths, so the remaining `skip_items` refactor alone does not justify a PR. Thanks for the thorough check!

Pull request closed

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!2575
No description provided.