HLE: check null and invalid handle in display deferral #531

Closed
pushkarreddyy wants to merge 1 commit from upstream-pr/531 into main
pushkarreddyy commented 2026-11-24 04:32:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-24T04:32:34Z.


Motivation

In crates/victory-hle/src/modules/display.rs:

  • acquire_deferral only checked request as isize == -1 after PowerCreateRequest(&context). In the Windows API, handle failure may return null (0) or INVALID_HANDLE_VALUE (-1). Checking both guarantees a null handle is not treated as a successful power request.
  • close_deferral checked !handle.is_null() but did not guard against pseudo-handle or invalid handle -1, which would invoke CloseHandle(-1).

Solution

  • Guard PowerCreateRequest result with request.is_null() || request as isize == -1.
  • Guard close_deferral with !handle.is_null() && handle as isize != -1.
  • Added unit tests for null output rejection in acquire_deferral and graceful null/invalid handling in close_deferral.

No game files used.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574667654 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-24T04:32:34Z. --- ## Motivation In `crates/victory-hle/src/modules/display.rs`: - `acquire_deferral` only checked `request as isize == -1` after `PowerCreateRequest(&context)`. In the Windows API, handle failure may return null (`0`) or `INVALID_HANDLE_VALUE` (`-1`). Checking both guarantees a null handle is not treated as a successful power request. - `close_deferral` checked `!handle.is_null()` but did not guard against pseudo-handle or invalid handle `-1`, which would invoke `CloseHandle(-1)`. ## Solution - Guard `PowerCreateRequest` result with `request.is_null() || request as isize == -1`. - Guard `close_deferral` with `!handle.is_null() && handle as isize != -1`. - Added unit tests for null output rejection in `acquire_deferral` and graceful null/invalid handling in `close_deferral`. No game files used.
pushkarreddyy commented 2026-11-24 06:55:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-24T04:51:26Z.


### Technical Verification & Implementation Details

Context & Root Cause

In crates/victory-hle/src/modules/display.rs:

  • acquire_deferral only checked request as isize == -1 after calling PowerCreateRequest(&context). In the Windows API, handle failure may return null (0) or INVALID_HANDLE_VALUE (-1). Checking both ensures null handles are not treated as valid power requests.
  • close_deferral checked !handle.is_null() but did not guard against pseudo/invalid handle -1, which would attempt an invalid CloseHandle(-1) call.

Changes & Diff

  • Guarded PowerCreateRequest result with request.is_null() || request as isize == -1.
  • Guarded close_deferral with !handle.is_null() && handle as isize != -1.
  • Added unit tests acquire_deferral_rejects_null_out and close_deferral_handles_null_and_invalid.

Git Log Line & Verification

ext 7f0053b HLE: check null and invalid handle in display deferral crates/victory-hle/src/modules/display.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)

  • Passes cargo xtask policy (Rule #1 compliance, zero game assets).
  • Formatted with rustfmt --edition 2024.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7122400529 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531#issuecomment-7122400529). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-24T04:51:26Z. --- ### Technical Verification & Implementation Details #### Context & Root Cause In `crates/victory-hle/src/modules/display.rs`: - `acquire_deferral` only checked `request as isize == -1` after calling `PowerCreateRequest(&context)`. In the Windows API, handle failure may return null (`0`) or `INVALID_HANDLE_VALUE` (`-1`). Checking both ensures null handles are not treated as valid power requests. - `close_deferral` checked `!handle.is_null()` but did not guard against pseudo/invalid handle `-1`, which would attempt an invalid `CloseHandle(-1)` call. #### Changes & Diff - Guarded `PowerCreateRequest` result with `request.is_null() || request as isize == -1`. - Guarded `close_deferral` with `!handle.is_null() && handle as isize != -1`. - Added unit tests `acquire_deferral_rejects_null_out` and `close_deferral_handles_null_and_invalid`. #### Git Log Line & Verification ` ext 7f0053b HLE: check null and invalid handle in display deferral crates/victory-hle/src/modules/display.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) ` - Passes `cargo xtask policy` (Rule #1 compliance, zero game assets). - Formatted with `rustfmt --edition 2024`.
pushkarreddyy commented 2026-11-24 06:56:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-24T06:24:07Z.


### Call for Testing & Maintainer Review
Community members testing game initialization or HLE modules on Windows 10/11: verification and feedback on this PR is warmly welcome!

  • Rule #1 asset policy compliant (cargo xtask policy passing).
  • Formatted with Rust 2024.

@Marvisak: Ready for maintainer review and merge when convenient!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123894534 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531#issuecomment-7123894534). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-24T06:24:07Z. --- ### Call for Testing & Maintainer Review Community members testing game initialization or HLE modules on Windows 10/11: verification and feedback on this PR is warmly welcome! - Rule #1 asset policy compliant (`cargo xtask policy` passing). - Formatted with Rust 2024. @Marvisak: Ready for maintainer review and merge when convenient!
mercuryVM commented 2026-11-24 08:02:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:37:38Z.

Review: APPROVED


Tested 7f0053b on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (266 passed, 0 failed).

  • PowerCreateRequest documents INVALID_HANDLE_VALUE on failure. Also rejecting null is harmless defensiveness, since a null request would only fail later in PowerSetRequest.
  • Not calling CloseHandle(-1) is right: -1 is the current-process pseudo-handle, and closing it is a no-op error at best.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035357165 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531#pullrequestreview-6035357165). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:37:38Z. **Review:** APPROVED --- Tested 7f0053b on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (266 passed, 0 failed). - `PowerCreateRequest` documents `INVALID_HANDLE_VALUE` on failure. Also rejecting null is harmless defensiveness, since a null request would only fail later in `PowerSetRequest`. - Not calling `CloseHandle(-1)` is right: `-1` is the current-process pseudo-handle, and closing it is a no-op error at best. No issues found. AI-assisted (Claude Code).
pushkarreddyy commented 2026-11-24 09:06:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-24T08:20:39Z.


Thank you @mercuryVM for testing and verifying on Windows 11 / MSVC! Appreciate the confirmation and approval on the handle validation.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7126019287 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531#issuecomment-7126019287). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-24T08:20:39Z. --- Thank you @mercuryVM for testing and verifying on Windows 11 / MSVC! Appreciate the confirmation and approval on the handle validation.
upstream-sync closed this pull request 2026-11-25 00:05:35 +00:00
Marvisak commented 2026-11-25 00:06:58 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current).

No observable effect: a null request from PowerCreateRequest already fails at PowerSetRequest and returns an error, and -1 is never handed out as a deferral handle. The new tests cover behaviour main already has.

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:7148097726 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/531#issuecomment-7148097726). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:03:53Z. --- Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current). No observable effect: a null request from PowerCreateRequest already fails at PowerSetRequest and returns an error, and -1 is never handed out as a deferral handle. The new tests cover behaviour main already has. 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!531
No description provided.