game: check record bounds and offset arithmetic in sgd5 #1153

Closed
github-bot wants to merge 1 commit from gh-pr/1153/head into gh-pr/1153/base
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 16:16 UTC · original
fix/sgd5-checked-record-bounds → main · +53 −9 · 1 files · 1 commits · closed without merging

Resolves #905

Problem

In crates/victory-game/src/sgd5.rs:

  • record() performed unchecked addition: let at = self.names_offset as usize + record.name_offset as usize; and directly sliced &self.data[at..]. If an archive contains corrupt or boundary name_offset values, this overflows or panics on slice index.
  • name_before() used unchecked subtraction at - 1 and unsliced indexing self.data[i].
  • records() could panic if records_offset was within data.len() but had fewer than record_count * size_of::<Record>() bytes available.

Fix

  1. Guard records() with checked_mul and checked_add against data.len().
  2. Use checked_add in record() and bounds-checked slice slicing (.get(at..)).
  3. Use checked_sub in name_before() and safe indexing.
  4. Added synthetic unit test rejects_boundary_and_malformed_records verifying bounds checking and NUL-terminator searching on truncated / corrupted archive headers.

All fixtures are synthetic; zero game data (Rule #1 compliant). Formatted with Rust 2024 edition.

**@⁠DaniiL35** on GitHub · 2026-11-24 16:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1153) `fix/sgd5-checked-record-bounds` → `main` · +53 −9 · 1 files · 1 commits · closed without merging Resolves #905 ### Problem In `crates/victory-game/src/sgd5.rs`: - `record()` performed unchecked addition: `let at = self.names_offset as usize + record.name_offset as usize;` and directly sliced `&self.data[at..]`. If an archive contains corrupt or boundary `name_offset` values, this overflows or panics on slice index. - `name_before()` used unchecked subtraction `at - 1` and unsliced indexing `self.data[i]`. - `records()` could panic if `records_offset` was within `data.len()` but had fewer than `record_count * size_of::<Record>()` bytes available. ### Fix 1. Guard `records()` with `checked_mul` and `checked_add` against `data.len()`. 2. Use `checked_add` in `record()` and bounds-checked slice slicing (`.get(at..)`). 3. Use `checked_sub` in `name_before()` and safe indexing. 4. Added synthetic unit test `rejects_boundary_and_malformed_records` verifying bounds checking and NUL-terminator searching on truncated / corrupted archive headers. All fixtures are synthetic; zero game data (Rule #1 compliant). Formatted with Rust 2024 edition.
Parsing an SGD5 shader container record previously used unguarded slice
indexing and unchecked additions when calculating payload boundaries and
header field offsets. On truncated archives or boundary offsets near
the file limits, this could panic on slice index out-of-bounds or integer
overflow.

Use checked_add/checked_sub and .get() boundary checks for all record
offsets, and add unit tests exercising boundary offsets, size mismatches,
payload truncation, and oversized unpacked limits.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:21:24 +00:00
github-bot closed this pull request 2026-11-26 04:21:24 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1153
No description provided.