XGameSave: a failed create leaves a null handle, as documented #228
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!228
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@Dufftwt on GitHub · 2026-11-24 01:38 UTC · original
Dufftwt:fix/xgamesave-null-handles-on-failure→main· +46 −0 · 1 files · 1 commits · openWhat'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.
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.@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
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.
@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_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.