Kernel: a title image's file name that fits clears the last error #186

Manually merged
github merged 1 commit from gh-pr/186/head into gh-pr/186/base 2026-11-26 03:53:51 +00:00
Owner

@⁠Maraco91 on GitHub · 2026-11-24 01:15 UTC · original
Maraco91:kernel/module-file-name-last-error → main · +49 −1 · 1 files · 1 commits · merged 2026-11-25 00:08 UTC

What

GetModuleFileNameA/W for a title image (the exe by NULL, or a title DLL) answers from our own record of its original path. copy_truncated sets ERROR_INSUFFICIENT_BUFFER when the name is cut, but leaves the thread's last error alone when the name fits. The host clears it on success, and callers rely on that, because the return value alone cannot tell "cut" from "filled the buffer exactly":

do { grow(buf); n = GetModuleFileNameW(NULL, buf, size); }
while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);          // never ends

n = GetModuleFileNameA(NULL, buf, MAX_PATH);
if (n == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) fail();   // fails on a whole name

With our answer, the first loop spins forever once its first buffer was too small, and the second fails whenever some earlier call on the thread left 122 behind. Other modules' names go to the host and were already right; only title images took this path. xgame.rs notes that Grand Theft Auto VI reads its own path with GetModuleFileNameA; I do not know which of these shapes its code has (I have no game files).

A name that fits now sets the last error to 0. The truncated and zero-size cases are unchanged.

Tested

Windows 11 Pro 10.0.26200, rustc 1.98.1 (x86_64-pc-windows-msvc), no game data, no GPU involved.

New unit test a_module_name_that_fits_clears_the_last_error. It first pins down what the host does, on a host module (kernel32.dll, which our functions pass through): with the last error preset to 122, a successful GetModuleFileNameW and GetModuleFileNameA both leave it at 0 on this machine. Then the same for a title image: too small (returns the size, last error 122, as before), then with room, wide and narrow, and an exact fit (17 characters in 18); control: 17 characters in 17 is still cut and still reports 122.

  • main (d2a4bae) + the test only: cargo test -p victory-hle --lib a_module_name_that_fits fails at the first title-image check: a title image, wide: left: 122, right: 0. The two host checks before it pass.
  • with the fix: cargo test -p victory-hle --lib kernel::: 4 passed, 0 failed.
  • cargo clippy -p victory-hle --tests: nothing in kernel.rs. rustfmt --check kernel.rs: clean.
  • cargo xtask policy --staged: ok.

Not tested: a title run; Windows 10 (the reset on success dates from Vista as far as I know, but I only ran Windows 11).

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

**@⁠Maraco91** on GitHub · 2026-11-24 01:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/186) `Maraco91:kernel/module-file-name-last-error` → `main` · +49 −1 · 1 files · 1 commits · merged 2026-11-25 00:08 UTC ## What `GetModuleFileNameA/W` for a title image (the exe by `NULL`, or a title DLL) answers from our own record of its original path. `copy_truncated` sets `ERROR_INSUFFICIENT_BUFFER` when the name is cut, but leaves the thread's last error alone when the name fits. The host clears it on success, and callers rely on that, because the return value alone cannot tell "cut" from "filled the buffer exactly": ```c do { grow(buf); n = GetModuleFileNameW(NULL, buf, size); } while (GetLastError() == ERROR_INSUFFICIENT_BUFFER); // never ends n = GetModuleFileNameA(NULL, buf, MAX_PATH); if (n == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) fail(); // fails on a whole name ``` With our answer, the first loop spins forever once its first buffer was too small, and the second fails whenever some earlier call on the thread left 122 behind. Other modules' names go to the host and were already right; only title images took this path. `xgame.rs` notes that Grand Theft Auto VI reads its own path with `GetModuleFileNameA`; I do not know which of these shapes its code has (I have no game files). A name that fits now sets the last error to 0. The truncated and zero-size cases are unchanged. ## Tested Windows 11 Pro 10.0.26200, rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data, no GPU involved. New unit test `a_module_name_that_fits_clears_the_last_error`. It first pins down what the host does, on a host module (`kernel32.dll`, which our functions pass through): with the last error preset to 122, a successful `GetModuleFileNameW` and `GetModuleFileNameA` both leave it at 0 on this machine. Then the same for a title image: too small (returns the size, last error 122, as before), then with room, wide and narrow, and an exact fit (17 characters in 18); control: 17 characters in 17 is still cut and still reports 122. - `main` (d2a4bae) + the test only: `cargo test -p victory-hle --lib a_module_name_that_fits` **fails** at the first title-image check: `a title image, wide: left: 122, right: 0`. The two host checks before it pass. - with the fix: `cargo test -p victory-hle --lib kernel::`: **4 passed, 0 failed**. - `cargo clippy -p victory-hle --tests`: nothing in `kernel.rs`. `rustfmt --check kernel.rs`: clean. - `cargo xtask policy --staged`: ok. Not tested: a title run; Windows 10 (the reset on success dates from Vista as far as I know, but I only ran Windows 11). AI-assisted (Claude Code); the tests above were run on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
GetModuleFileNameA/W on the host clears the thread's last error when
the name fits, and code tells a truncated name from a whole one by it
(grow the buffer while the last error is ERROR_INSUFFICIENT_BUFFER; or
fail if it is, also when the name filled the buffer exactly). Our
answer for a title image set the error when the name was cut and left
it alone otherwise, so after one call with too small a buffer, or
after any earlier call on the thread that left 122, a whole name still
read as truncated.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:49 +00:00
github manually merged commit 3c7d845b3b into gh-pr/186/base 2026-11-26 03:53:51 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:24 UTC · original

Tested 3c7d845 under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle and a native Microsoft d3dcompiler_47: pass.

  • victory_hle: 86 passed, 1 failed — the failure is modules::net::tests::only_loopback_is_reachable (Wine lacks WSAConnectByList, as in #118), not this PR. The test this PR adds passes.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/186#issuecomment-7119063272) Tested `3c7d845` under **Wine 11.22 + vkd3d-proton 3.1** on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with `cargo xwin ... --target x86_64-pc-windows-msvc -p victory-hle` and a native Microsoft `d3dcompiler_47`: **pass**. - `victory_hle`: **86 passed, 1 failed** — the failure is `modules::net::tests::only_loopback_is_reachable` (Wine lacks `WSAConnectByList`, as in #118), not this PR. The test this PR adds passes. No game data used.
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!186
No description provided.