xgameruntime_gamesave: prevent uint64_t quota underflow in commit() on case-colliding blobs #147

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

@⁠Andretuta on GitHub · 2026-11-24 02:28 UTC · original
ubiquitarianism:old-pr/9880 → main · +76 −3 · 2 files · 1 commits · draft

Carried over from old PR 9880 by ChalanaGimhanaX in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9880, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
ChalanaGimhanaX: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116740917

(Resubmission of #8855, which was previously approved by @abnalstlll-byte and verified by @smowlyy before being closed during the repo transfer cleanup; cleanly rebased onto ProjectLeonida/Leonida main 7e39a81ea and re-verified.)

What and why

In runtime/shims/xgameruntime_gamesave.cpp, commit() computes the post-update quota by iterating u->ops and subtracting each existing file's size from uint64_t after = used_bytes(c->user_dir):

const uint64_t old = fs::is_regular_file(p, ec) ? static_cast<uint64_t>(fs::file_size(p, ec)) : 0;
after = after - old + (op.write ? op.data.size() : 0);

On Windows NTFS (case-insensitive), if a single XGameSaveUpdate references two blob names that differ only in ASCII case (e.g., deleting or updating "story" and "STORY" when "story" exists on disk), both paths resolve to the same existing file. Subtracting its size twice underflows uint64_t after to ~UINT64_MAX and causes a tiny update or even a pure delete to fail with a bogus E_GS_QUOTA_EXCEEDED. Likewise, if fs::file_size(p, ec) fails and sets ec, it returns static_cast<uintmax_t>(-1), which also wrapped after.

Changes:

  • In commit() (quota calculation loop only):
    • Check !size_ec && sz != static_cast<uintmax_t>(-1) before subtracting an existing file's size.
    • Track accounted paths and skip re-subtracting files where fs::equivalent(prev, p, eq_ec) is true so each on-disk file is subtracted at most once.
    • Use saturating subtraction and saturating addition on after.
  • Add regression checks in runtime/test/xgameruntime_gamesave_test.cpp for case-colliding deletes and write + case-colliding delete in a single update.

Verification (rebased onto 7e39a81ea)

Windows 11 x64, MSVC 19.50 (Visual Studio 18 Community), Windows SDK 10.0.26100.0, Release x64, CPU-only synthetic tests (no game files):

  • Negative control (new checks against unpatched main): xgameruntime_gamesave_test failed 4 of 66 checks (both case-colliding deletes and write+delete returned E_GS_QUOTA_EXCEEDED, leaving the blob on disk).
  • With fix (rebased on 7e39a81ea):
    • ctest --test-dir runtime/build -C Release -R ^xgameruntime_gamesave$ --output-on-failure -V: 1/1 passed (xgameruntime_gamesave_test: 66 checks, 0 failures, 0.16 sec)
    • python -I scripts/check_no_game_data.py runtime/shims/xgameruntime_gamesave.cpp runtime/test/xgameruntime_gamesave_test.cpp: exit 0
    • git diff origin/main..HEAD --check: exit 0 (LF only, 0 CRLF)

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A — offline XGameSave runtime shim fix, not a translator rule.)
**@⁠Andretuta** on GitHub · 2026-11-24 02:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/147) `ubiquitarianism:old-pr/9880` → `main` · +76 −3 · 2 files · 1 commits · draft > Carried over from **old PR 9880** by `ChalanaGimhanaX` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9880`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `ChalanaGimhanaX`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116740917 *(Resubmission of `#8855`, which was previously approved by `@abnalstlll-byte` and verified by `@smowlyy` before being closed during the repo transfer cleanup; cleanly rebased onto `ProjectLeonida/Leonida` `main` `7e39a81ea` and re-verified.)* ### What and why In `runtime/shims/xgameruntime_gamesave.cpp`, `commit()` computes the post-update quota by iterating `u->ops` and subtracting each existing file's size from `uint64_t after = used_bytes(c->user_dir)`: ```cpp const uint64_t old = fs::is_regular_file(p, ec) ? static_cast<uint64_t>(fs::file_size(p, ec)) : 0; after = after - old + (op.write ? op.data.size() : 0); ``` On Windows NTFS (case-insensitive), if a single `XGameSaveUpdate` references two blob names that differ only in ASCII case (e.g., deleting or updating `"story"` and `"STORY"` when `"story"` exists on disk), both paths resolve to the same existing file. Subtracting its size twice underflows `uint64_t after` to `~UINT64_MAX` and causes a tiny update or even a pure delete to fail with a bogus `E_GS_QUOTA_EXCEEDED`. Likewise, if `fs::file_size(p, ec)` fails and sets `ec`, it returns `static_cast<uintmax_t>(-1)`, which also wrapped `after`. Changes: - In `commit()` (quota calculation loop only): - Check `!size_ec && sz != static_cast<uintmax_t>(-1)` before subtracting an existing file's size. - Track accounted paths and skip re-subtracting files where `fs::equivalent(prev, p, eq_ec)` is true so each on-disk file is subtracted at most once. - Use saturating subtraction and saturating addition on `after`. - Add regression checks in `runtime/test/xgameruntime_gamesave_test.cpp` for case-colliding deletes and write + case-colliding delete in a single update. ### Verification (rebased onto `7e39a81ea`) Windows 11 x64, MSVC 19.50 (Visual Studio 18 Community), Windows SDK 10.0.26100.0, Release x64, CPU-only synthetic tests (no game files): - **Negative control** (new checks against unpatched `main`): `xgameruntime_gamesave_test` failed 4 of 66 checks (both case-colliding deletes and write+delete returned `E_GS_QUOTA_EXCEEDED`, leaving the blob on disk). - **With fix (rebased on `7e39a81ea`)**: - `ctest --test-dir runtime/build -C Release -R ^xgameruntime_gamesave$ --output-on-failure -V`: **1/1 passed** (`xgameruntime_gamesave_test: 66 checks, 0 failures`, `0.16 sec`) - `python -I scripts/check_no_game_data.py runtime/shims/xgameruntime_gamesave.cpp runtime/test/xgameruntime_gamesave_test.cpp`: exit 0 - `git diff origin/main..HEAD --check`: exit 0 (LF only, 0 CRLF) AI-assisted: yes ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. *(N/A — offline XGameSave runtime shim fix, not a translator rule.)*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:46:23 +00:00
This pull request has changes conflicting with the target branch.
  • runtime/test/xgameruntime_gamesave_test.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-147'
git switch 'github-bot/gh-147'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-147'
git switch 'github-bot/gh-147'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-147'
git switch 'main'
git merge --squash 'github-bot/gh-147'
git switch 'main'
git merge --ff-only 'github-bot/gh-147'
git switch 'main'
git merge 'github-bot/gh-147'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.