XGameSave: a failed create leaves a null handle, as documented #228

Open
github-bot wants to merge 1 commit from github-bot/gh-228 into main AGit
Owner

@⁠Dufftwt on GitHub · 2026-11-24 01:38 UTC · original
Dufftwt:fix/xgamesave-null-handles-on-failure → main · +46 −0 · 1 files · 1 commits · open

What's wrong

Three XGameSave calls hand the title a handle through an out-parameter:

  • XGameSaveInitializeProviderResult
  • XGameSaveCreateContainer
  • XGameSaveCreateUpdate

When they fail, our implementations return the error and leave the title's variable as it was.

The GDK declares all three out-parameters _Outptr_result_nullonfailure_: on failure the handle must be null. Titles rely on that. The synchronous sample on the XGameSaveCreateUpdate page declares XGameSaveContainerHandle containerContext; and XGameSaveUpdateHandle updateContext; without initializing them. Whatever happens, it then cleans up with if (updateContext) XGameSaveCloseUpdate(updateContext); and the same for the container.

With our implementation, a failed call leaves stack garbage in the variable. The title then passes it to the matching XGameSaveClose*, and that ends in Box::from_raw on an address we never allocated. The result is heap corruption or a crash, somewhere far from the failed call that caused it:

XGameSaveCreateContainer(provider, "bad name", &container)  -> E_GS_INVALID_CONTAINER_NAME, container = <stack garbage>
XGameSaveCloseContainer(container)                          -> frees <stack garbage>

A provider that can't open its folder (E_GS_NO_ACCESS, e.g. the data folder can't be created) does the same through XGameSaveInitializeProviderResult.

What the fix does

The three functions now set the title's handle to null first, before anything can fail. A small helper, clear, does it. A successful call overwrites it with the real handle as before. A failed call leaves null, so closing it is a no-op, as the take helper already treats a null handle.

How it behaves after the fix

  • A failed provider, container or update creation returns the same error as before, and the title's handle is null.
  • Code that closes whatever handle it holds, like the GDK sample, no longer frees garbage.
  • Successful calls don't change.

Tests

The new test a_failed_create_leaves_a_null_handle starts each out-parameter at a garbage value (0x1234), as an uninitialized variable would hold:

  • XGameSaveCreateContainer with an invalid name returns E_GS_INVALID_CONTAINER_NAME.
  • XGameSaveCreateUpdate with no container returns E_INVALIDARG.
  • XGameSaveInitializeProviderResult on a provider call that failed with E_GS_NO_ACCESS returns E_GS_NO_ACCESS.

Each must leave the handle null. The test then closes all three the way the sample does.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes. victory-hle has 87 passed, the other crates have 0 failed.
  • The new test alone on main: it fails at the first check. After a failed XGameSaveCreateContainer the handle still holds the garbage value.
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodified main as well.

Not tested: the title's own save code.

**@⁠Dufftwt** on GitHub · 2026-11-24 01:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/228) `Dufftwt:fix/xgamesave-null-handles-on-failure` → `main` · +46 −0 · 1 files · 1 commits · open ## What's wrong Three XGameSave calls hand the title a handle through an out-parameter: - `XGameSaveInitializeProviderResult` - `XGameSaveCreateContainer` - `XGameSaveCreateUpdate` When they fail, our implementations return the error and leave the title's variable as it was. The GDK declares all three out-parameters `_Outptr_result_nullonfailure_`: on failure the handle must be null. Titles rely on that. The synchronous sample on the [`XGameSaveCreateUpdate`](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/system/xgamesave/functions/xgamesavecreateupdate) page declares `XGameSaveContainerHandle containerContext;` and `XGameSaveUpdateHandle updateContext;` without initializing them. Whatever happens, it then cleans up with `if (updateContext) XGameSaveCloseUpdate(updateContext);` and the same for the container. With our implementation, a failed call leaves stack garbage in the variable. The title then passes it to the matching `XGameSaveClose*`, and that ends in `Box::from_raw` on an address we never allocated. The result is heap corruption or a crash, somewhere far from the failed call that caused it: ``` XGameSaveCreateContainer(provider, "bad name", &container) -> E_GS_INVALID_CONTAINER_NAME, container = <stack garbage> XGameSaveCloseContainer(container) -> frees <stack garbage> ``` A provider that can't open its folder (`E_GS_NO_ACCESS`, e.g. the data folder can't be created) does the same through `XGameSaveInitializeProviderResult`. ## What the fix does The three functions now set the title's handle to null first, before anything can fail. A small helper, `clear`, does it. A successful call overwrites it with the real handle as before. A failed call leaves null, so closing it is a no-op, as the `take` helper already treats a null handle. ## How it behaves after the fix - A failed provider, container or update creation returns the same error as before, and the title's handle is null. - Code that closes whatever handle it holds, like the GDK sample, no longer frees garbage. - Successful calls don't change. ## Tests The new test `a_failed_create_leaves_a_null_handle` starts each out-parameter at a garbage value (`0x1234`), as an uninitialized variable would hold: - `XGameSaveCreateContainer` with an invalid name returns `E_GS_INVALID_CONTAINER_NAME`. - `XGameSaveCreateUpdate` with no container returns `E_INVALIDARG`. - `XGameSaveInitializeProviderResult` on a provider call that failed with `E_GS_NO_ACCESS` returns `E_GS_NO_ACCESS`. Each must leave the handle null. The test then closes all three the way the sample does. I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes. `victory-hle` has 87 passed, the other crates have 0 failed. - **The new test alone on `main`:** it fails at the first check. After a failed `XGameSaveCreateContainer` the handle still holds the garbage value. - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (`STATUS_ACCESS_VIOLATION`), on unmodified `main` as well. **Not tested:** the title's own save code.
XGameSaveCreateContainer, XGameSaveCreateUpdate and
XGameSaveInitializeProviderResult left the title's handle untouched when
they failed. The GDK documents those handles as null on failure, and
its own sample closes them on that basis ("if (containerContext)
XGameSaveCloseContainer(...)" on a variable it never initialized), so
a failed call made the title close whatever was on its stack: a heap
corruption far from the failure that caused it.

Each of the three now nulls the handle before anything can fail.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:36 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 05:26 UTC · original

Thanks @⁠Dufftwt for making failed XGameSave create operations leave the output handles safely null.

I tested commit 09ff84b6a7cb5cc6f612697866113d3d791ab2ae with:

cargo test --locked -p victory-hle --lib a_failed_create_leaves_a_null_handle -- --test-threads=1

The targeted test passed (1 passed, 0 failed; 86 filtered). a_failed_create_leaves_a_null_handle initializes outputs to non-null sentinels, then checks that an invalid container name, a null provider, and a failed provider initialization return their expected errors and replace the outputs with null. It also verifies closing those null handles is safe.

This exercises the HLE handle/error paths with local fixtures; it does not call a real title or storage service.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/228#issuecomment-7122951007) Thanks @⁠Dufftwt for making failed XGameSave create operations leave the output handles safely null. I tested commit `09ff84b6a7cb5cc6f612697866113d3d791ab2ae` with: ``` cargo test --locked -p victory-hle --lib a_failed_create_leaves_a_null_handle -- --test-threads=1 ``` The targeted test passed (1 passed, 0 failed; 86 filtered). [`a_failed_create_leaves_a_null_handle`](https://github.com/Marvisak/VIctory/blob/09ff84b6a7cb5cc6f612697866113d3d791ab2ae/crates/victory-hle/src/modules/xgamesave.rs#L1122) initializes outputs to non-null sentinels, then checks that an invalid container name, a null provider, and a failed provider initialization return their expected errors and replace the outputs with null. It also verifies closing those null handles is safe. This exercises the HLE handle/error paths with local fixtures; it does not call a real title or storage service.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:26 UTC · original

Automated triage for the maintainers.

Needs work: reword the comment. The fix is right: initialize_result, create_container and create_update in crates/victory-hle/src/modules/xgamesave.rs null the title's out-handle before anything can fail, and take already treats null as a no-op, so a title that closes what it holds no longer frees garbage. The test is good.

The doc comment on the new clear helper names the console SDK and quotes an annotation copied from its headers, which the repo rules don't allow. Please reword it in plain terms, e.g. "a handle the title asked for is null when the call fails; titles close whatever handle they hold." With that, this would be a merge candidate.

CI: no check runs on the head commit.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/228#issuecomment-7148514273) <!-- victory-triage v1 head=09ff84b6a7cb5cc6f612697866113d3d791ab2ae verdict=needs-work at=2026-11-25T00:18:46Z --> Automated triage for the maintainers. **Needs work: reword the comment.** The fix is right: `initialize_result`, `create_container` and `create_update` in `crates/victory-hle/src/modules/xgamesave.rs` null the title's out-handle before anything can fail, and `take` already treats null as a no-op, so a title that closes what it holds no longer frees garbage. The test is good. The doc comment on the new `clear` helper names the console SDK and quotes an annotation copied from its headers, which the repo rules don't allow. Please reword it in plain terms, e.g. "a handle the title asked for is null when the call fails; titles close whatever handle they hold." With that, this would be a merge candidate. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/228/head:github-bot/gh-228'
git switch 'github-bot/gh-228'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-228'
git switch 'github-bot/gh-228'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-228'
git switch 'github-bot/gh-228'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-228'
git switch 'main'
git merge --squash 'github-bot/gh-228'
git switch 'main'
git merge --ff-only 'github-bot/gh-228'
git switch 'main'
git merge 'github-bot/gh-228'
git push origin 'main'
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!228
No description provided.