Game: check record bounds and offset arithmetic in SGD5 parsing #1435

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/1436 into main
fuzeofficlal commented 2026-11-24 17:58:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-24T17:58:20Z.


Fixes #905.

Summary

  • Replace unchecked slice indexing and offset arithmetic in sgd5::record and sgd5::name_before with checked_add, checked_sub, and .get(...) bounds checks so truncated or malformed SGD5 streams return None instead of panicking.

Verification

  • Added rejects_boundary_and_malformed_records covering short prefixes (at < 6), truncated headers/payloads, mismatched lengths, u32::MAX / usize::MAX offsets, and malformed name prefixes.
  • cargo test --workspace, cargo clippy --workspace --all-targets --all-features, and cargo xtask policy: passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6588135498 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1436). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-24T17:58:20Z. --- Fixes #905. ### Summary - Replace unchecked slice indexing and offset arithmetic in `sgd5::record` and `sgd5::name_before` with `checked_add`, `checked_sub`, and `.get(...)` bounds checks so truncated or malformed `SGD5` streams return `None` instead of panicking. ### Verification - Added `rejects_boundary_and_malformed_records` covering short prefixes (`at < 6`), truncated headers/payloads, mismatched lengths, `u32::MAX` / `usize::MAX` offsets, and malformed name prefixes. - `cargo test --workspace`, `cargo clippy --workspace --all-targets --all-features`, and `cargo xtask policy`: passed.
catminou4 commented 2026-11-24 20:02:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:25:06Z.

Review: APPROVED


SGD5 record parsing is now panic-free at the boundaries: every offset goes through checked_add/checked_sub, file.get replaces direct indexing (including the NUL byte and name-length checks in name_before), and truncated/oversized/mismatched records return None. The new rejects_boundary_and_malformed_records test covers marker-at-start, near-start padding, truncation, n < 12, mismatched n, oversized unpacked_size, u32::MAX sizes and extreme synthetic offsets — verified running on macOS arm64 (cargo test -p victory-game --locked: 3 passed, 0 failed, +1).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045877630 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1436#pullrequestreview-6045877630). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T19:25:06Z. **Review:** APPROVED --- SGD5 record parsing is now panic-free at the boundaries: every offset goes through `checked_add`/`checked_sub`, `file.get` replaces direct indexing (including the NUL byte and name-length checks in `name_before`), and truncated/oversized/mismatched records return `None`. The new `rejects_boundary_and_malformed_records` test covers marker-at-start, near-start padding, truncation, `n < 12`, mismatched `n`, oversized `unpacked_size`, `u32::MAX` sizes and extreme synthetic offsets — verified running on macOS arm64 (`cargo test -p victory-game --locked`: 3 passed, 0 failed, +1).
Marvisak commented 2026-11-25 02:06:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:28:14Z.


Automated triage for the maintainers.

Proposed close: guards for cases that cannot occur. In crates/victory-game/src/sgd5.rs on main, record() has a single caller, records(), which passes at from memmem::find_iter(file, &MARKER). So at + 4 <= file.len(), and at + 4, at + 8, at + 12 and at + n (with n from a u32) cannot overflow usize. The existing at.checked_sub(6)? already returns before file[nul] and file[at - 5] could go out of range. file.get(at + 12..at + n) already rejects a truncated payload. name_before is only called with nul < at <= file.len(), so its indices are in range too. Every records(..) assertion in the new test also holds on main. The calls that differ (record(&MARKER, usize::MAX), name_before(&[0], 5)) use arguments no caller can pass. The same change was closed before as #1153.

A malformed input that makes main panic through records() would change this verdict.

CI: passing (check).

This pull request will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149582056 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1436#issuecomment-7149582056). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:28:14Z. --- <!-- victory-triage v1 head=4d9539023eafb82fcb708a719f62eb09edfb1a07 verdict=proposed-close at=2026-11-25T06:30:22Z --> Automated triage for the maintainers. **Proposed close: guards for cases that cannot occur.** In `crates/victory-game/src/sgd5.rs` on `main`, `record()` has a single caller, `records()`, which passes `at` from `memmem::find_iter(file, &MARKER)`. So `at + 4 <= file.len()`, and `at + 4`, `at + 8`, `at + 12` and `at + n` (with `n` from a `u32`) cannot overflow `usize`. The existing `at.checked_sub(6)?` already returns before `file[nul]` and `file[at - 5]` could go out of range. `file.get(at + 12..at + n)` already rejects a truncated payload. `name_before` is only called with `nul < at <= file.len()`, so its indices are in range too. Every `records(..)` assertion in the new test also holds on `main`. The calls that differ (`record(&MARKER, usize::MAX)`, `name_before(&[0], 5)`) use arguments no caller can pass. The same change was closed before as #1153. A malformed input that makes `main` panic through `records()` would change this verdict. CI: passing (`check`). This pull request will be closed in 48 hours unless the author pushes a change 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-25 11:03:10 +00:00
fuzeofficlal commented 2026-11-25 11:05:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T10:16:54Z.


Agreed with the triage analysis — records() bounds at via memmem::find_iter and .get(...) on 64-bit usize, so the internal helper edge cases cannot be reached on main. Closing to keep the review queue focused.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159480045 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1436#issuecomment-7159480045). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T10:16:54Z. --- Agreed with the triage analysis — `records()` bounds `at` via `memmem::find_iter` and `.get(...)` on 64-bit `usize`, so the internal helper edge cases cannot be reached on `main`. Closing to keep the review queue focused.

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