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

Open
opened 2026-11-26 20:17:06 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/147
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9880


📝 Commits (1)

  • 9dca4f3 xgameruntime_gamesave: prevent uint64_t quota underflow in commit() on case-colliding blobs

📊 Changes

2 files changed (+76 additions, -3 deletions)

View changed files

📝 runtime/shims/xgameruntime_gamesave.cpp (+22 -3)
📝 runtime/test/xgameruntime_gamesave_test.cpp (+54 -0)

📄 Description

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.)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/147 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9880` --- ### 📝 Commits (1) - [`9dca4f3`](https://github.com/ubiquitarianism/leonida/commit/9dca4f37318f690c3bee6f2c3683b4d5526739ed) xgameruntime_gamesave: prevent uint64_t quota underflow in commit() on case-colliding blobs ### 📊 Changes **2 files changed** (+76 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/xgameruntime_gamesave.cpp` (+22 -3) 📝 `runtime/test/xgameruntime_gamesave_test.cpp` (+54 -0) </details> ### 📄 Description > 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.)* --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#199
No description provided.