XGameSave: a failed create leaves a null handle, as documented #228
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/228"
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 Dufftwt on 2026-11-24T01:38:30Z.
What's wrong
Three XGameSave calls hand the title a handle through an out-parameter:
XGameSaveInitializeProviderResultXGameSaveCreateContainerXGameSaveCreateUpdateWhen 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 theXGameSaveCreateUpdatepage declaresXGameSaveContainerHandle containerContext;andXGameSaveUpdateHandle updateContext;without initializing them. Whatever happens, it then cleans up withif (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 inBox::from_rawon an address we never allocated. The result is heap corruption or a crash, somewhere far from the failed call that caused it:A provider that can't open its folder (
E_GS_NO_ACCESS, e.g. the data folder can't be created) does the same throughXGameSaveInitializeProviderResult.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 thetakehelper already treats a null handle.How it behaves after the fix
Tests
The new test
a_failed_create_leaves_a_null_handlestarts each out-parameter at a garbage value (0x1234), as an uninitialized variable would hold:XGameSaveCreateContainerwith an invalid name returnsE_GS_INVALID_CONTAINER_NAME.XGameSaveCreateUpdatewith no container returnsE_INVALIDARG.XGameSaveInitializeProviderResulton a provider call that failed withE_GS_NO_ACCESSreturnsE_GS_NO_ACCESS.Each must leave the handle null. The test then closes all three the way the sample does.
I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-hlehas 87 passed, the other crates have 0 failed.main: it fails at the first check. After a failedXGameSaveCreateContainerthe handle still holds the garbage value.--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodifiedmainas well.Not tested: the title's own save code.
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:26:33Z.
Thanks @Dufftwt for making failed XGameSave create operations leave the output handles safely null.
I tested commit
09ff84b6a7cb5cc6f612697866113d3d791ab2aewith:The targeted test passed (1 passed, 0 failed; 86 filtered).
a_failed_create_leaves_a_null_handleinitializes 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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T08:45:58Z.
Review: APPROVED
Tested
09ff84bon Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (265 passed, 0 failed), includinga_failed_create_leaves_a_null_handle._Outptr_result_nullonfailure_is the documented contract, and your reading of the GDK sample (uninitialized handles, then unconditionalif (h) Close(h)) shows why it matters. Writing null first, before any early return, covers every failure path in all three functions.putalready ignores a nullout. On success the real handle overwrites the null as before.No issues found.
AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:26:34Z.
Automated triage for the maintainers.
Needs work: reword the comment. The fix is right:
initialize_result,create_containerandcreate_updateincrates/victory-hle/src/modules/xgamesave.rsnull the title's out-handle before anything can fail, andtakealready 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
clearhelper 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
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.