GPU: guard zero/overflowing stride in expand_rects and add primitive.rs unit tests #1158

Closed
Vittorio17 wants to merge 1 commit from upstream-pr/1158 into main
Vittorio17 commented 2026-11-24 16:18:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Vittorio17 on 2026-11-24T16:18:08Z.


Summary

Guards expand_rects in crates/victory-gpu/src/primitive.rs against a zero or overflowing stride, which previously panicked in chunks_exact(0) or in the 6 * stride arithmetic, and adds three deterministic synthetic unit tests (7 total in primitive.rs).

Closes #1061.

Changes

Bug fix: expand_rects now returns early when stride == 0 or when stride.checked_mul(3) / stride.checked_mul(6) overflows, before any slice arithmetic.

Tests added (+3 synthetic, 7 total):

  • zero_or_overflowing_stride_is_ignored_without_panicking: zero and usize::MAX / 2 strides leave the destination untouched.
  • unorm16_unorm8_clamping_and_unknown_formats_expand_correctly: UNORM16 arithmetic, UNORM8 clamping below 0 / above 1, and an unknown format keeping the first vertex's bytes.
  • multiple_rectangles_and_f32_f16_vector_formats_expand_in_sequence: two rectangles of R32G32B32_FLOAT plus R16G16_FLOAT, checking both fourth corners.

Synthetic tests only; no fixtures or game data.

Verification (Windows 11 x64 MSVC, Rust 1.98.1)

  • rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs: PASS
  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: PASS (0 warnings)
  • cargo clippy -p victory-gpu --all-targets --locked --features diagnostics -- -D warnings: PASS (0 warnings)
  • cargo test -p victory-gpu --locked -- "primitive::tests": 7 passed, 0 failed

Cherry-picked from geiyiliyabumo/VIctory@e3c9dd0 (branch test/gpu-primitive-audit), as suggested in the issue.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586315268 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1158). Originally posted by [Vittorio17](https://github.com/Vittorio17) on 2026-11-24T16:18:08Z. --- ## Summary Guards `expand_rects` in `crates/victory-gpu/src/primitive.rs` against a zero or overflowing stride, which previously panicked in `chunks_exact(0)` or in the `6 * stride` arithmetic, and adds three deterministic synthetic unit tests (7 total in `primitive.rs`). Closes #1061. ## Changes **Bug fix**: `expand_rects` now returns early when `stride == 0` or when `stride.checked_mul(3)` / `stride.checked_mul(6)` overflows, before any slice arithmetic. **Tests added** (+3 synthetic, 7 total): - `zero_or_overflowing_stride_is_ignored_without_panicking`: zero and `usize::MAX / 2` strides leave the destination untouched. - `unorm16_unorm8_clamping_and_unknown_formats_expand_correctly`: UNORM16 arithmetic, UNORM8 clamping below 0 / above 1, and an unknown format keeping the first vertex's bytes. - `multiple_rectangles_and_f32_f16_vector_formats_expand_in_sequence`: two rectangles of `R32G32B32_FLOAT` plus `R16G16_FLOAT`, checking both fourth corners. Synthetic tests only; no fixtures or game data. ## Verification (Windows 11 x64 MSVC, Rust 1.98.1) - `rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs`: PASS - `cargo xtask policy`: PASS (154 files ok) - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: PASS (0 warnings) - `cargo clippy -p victory-gpu --all-targets --locked --features diagnostics -- -D warnings`: PASS (0 warnings) - `cargo test -p victory-gpu --locked -- "primitive::tests"`: 7 passed, 0 failed Cherry-picked from `geiyiliyabumo/VIctory@e3c9dd0` (branch `test/gpu-primitive-audit`), as suggested in the issue.
DaniiL35 commented 2026-11-24 17:04:22 +00:00 (Migrated from github.com)

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


Independent Verification Review

Audited expand_rects arithmetic and slice chunking invariants in crates/victory-gpu/src/primitive.rs:

  1. Zero-Stride & Overflow Panic Invariant:

    • In the standard library, chunks_exact(0) and chunks_exact_mut(0) panic immediately (assert!(chunk_size != 0)).
    • If an untrusted title geometry packet supplies stride == 0 or an out-of-range stride where 3 * stride overflows usize, previous code panicked.
    • Early return via stride == 0 and checked_mul(3) / checked_mul(6) cleanly protects geometry expansion without allocating or panicking.
  2. Test Quality & Cross-Platform Execution:

    • The new tests (zero_or_overflowing_stride_is_ignored_without_panicking, unorm16_unorm8_clamping_and_unknown_formats_expand_correctly, and multiple_rectangles_and_f32_f16_vector_formats_expand_in_sequence) verify UNORM clamping, fourth-vertex extrapolation (v0 + v2 - v1), and raw fallback behavior.
    • All tests use synthetic in-memory vertex buffers, execute cleanly without host GPU/Windows dependencies, and pass Rule #1.

PR is clean, minimal, and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137463652 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1158#issuecomment-7137463652). Originally posted by [DaniiL35](https://github.com/DaniiL35) on 2026-11-24T16:18:53Z. --- ### Independent Verification Review Audited `expand_rects` arithmetic and slice chunking invariants in `crates/victory-gpu/src/primitive.rs`: 1. **Zero-Stride & Overflow Panic Invariant**: - In the standard library, `chunks_exact(0)` and `chunks_exact_mut(0)` panic immediately (`assert!(chunk_size != 0)`). - If an untrusted title geometry packet supplies `stride == 0` or an out-of-range stride where `3 * stride` overflows `usize`, previous code panicked. - Early return via `stride == 0` and `checked_mul(3)` / `checked_mul(6)` cleanly protects geometry expansion without allocating or panicking. 2. **Test Quality & Cross-Platform Execution**: - The new tests (`zero_or_overflowing_stride_is_ignored_without_panicking`, `unorm16_unorm8_clamping_and_unknown_formats_expand_correctly`, and `multiple_rectangles_and_f32_f16_vector_formats_expand_in_sequence`) verify UNORM clamping, fourth-vertex extrapolation (`v0 + v2 - v1`), and raw fallback behavior. - All tests use synthetic in-memory vertex buffers, execute cleanly without host GPU/Windows dependencies, and pass Rule #1. PR is clean, minimal, and ready to merge.
Zipettte commented 2026-11-24 18:06:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T18:01:07Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at abefa49: cargo test -p victory-gpu passes, 53 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140037453 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1158#issuecomment-7140037453). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T18:01:07Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at abefa49: `cargo test -p victory-gpu` passes, 53 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
upstream-sync closed this pull request 2026-11-25 01:05:43 +00:00
Marvisak commented 2026-11-25 01:08:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:33:02Z.


Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1204.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148624528 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1158#issuecomment-7148624528). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:33:02Z. --- Automated triage for the maintainers. Closing because another open pull request covers the same change. See #1204. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

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