Game: use checked length arithmetic and avoid discarded Vec allocations in sgd5 #2575
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2577"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:checked_mul/checked_addso malformed SGD5 payloads return a clean parse error instead of overflowing or panicking.Vecallocations 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)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.sgd5.rsto main and kept only the newrecord_rejects_overflowing_lengthtest. It still passes (all 5sgd5tests 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
The regression test passes without the fix. On a 64-bit target,
at + nwithn = u32::MAX as usizeand a smallatdoesn't overflowusize. On main,file.get(at + 12..at + n)?simply returnsNone, andrecordscomes 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.None of the checked arithmetic is reachable on the targets VIctory builds. VIctory builds x86-64 only (Windows MSVC, plus the Linux crates).
record(sgd5.rs:147-168), every operand isat < file.len()plus au32, which can't wrap a 64-bitusize.effect, every count that feedsloadedhas already been bounded byCursor::bytes, which useschecked_addandfile.get(sgd5.rs:188). Sos.len(),techniques.len(),passes.len()andextraare at most the file size.blocksis masked to 24 bits, and each remaining term is a singleu32.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.The allocation changes are fine.
skip_itemskeeps thechecked_mulbound ofitems.u32sviaas_chunks::<4>()has no remainder, because exactlyn * 4bytes are taken.extrais only ever used as a count. No behaviour change, and it saves two discardedVecs per effect.Not verified
Reviewed with Claude Code.
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). Insgd5::recordandsgd5::effect, every term added is either au32widened to a 64-bitusizeor the length of a slice already read from the file.at + 12,at + nand theloadedsum cannot overflow there. Onmainan oversized length already makesfile.get(..)/Cursor::bytesreturnNone. The new testrecord_rejects_overflowing_lengthpasses onmainunchanged for that reason, so it does not show a bug. What remains isskip_itemsin place of two discarded smallVecs 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 thetriage: proposed closelabel.Generated by Claude Code
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
usizeadditions insgd5cannot wrap andfile.get/Cursor::bytesalready reject oversized lengths, so the remainingskip_itemsrefactor alone does not justify a PR. Thanks for the thorough check!Pull request closed