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

Closed
DaniiL35 wants to merge 1 commit from upstream-pr/1153 into main
DaniiL35 commented 2026-11-24 16:16:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaniiL35 on 2026-11-24T16:16:21Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586280439 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1153). Originally posted by [DaniiL35](https://github.com/DaniiL35) on 2026-11-24T16:16:21Z. --- 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.
upstream-sync closed this pull request 2026-11-24 17:03:31 +00:00

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