From cda2be62455b0c889eb05d4214f4fe677690615f Mon Sep 17 00:00:00 2001 From: Maraco91 <43787609+Maraco91@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:09:22 +0300 Subject: [PATCH] HLE: a module name that fits clears the last error `GetModuleFileName`'s contract sets the last error on truncation and clears it when the whole name fits; `copy_truncated` only ever set it. A title that tells a truncated name from a whole one by the last error (its own check, after a buffer it grew until the call fit) read a stale error and treated a whole name as truncated. Ported from DexNom/VIctory 3c7d845b, GPL-3.0. (cherry picked from commit b998af5432fb5af2b66c2db504d386a3656bf095) --- crates/victory-hle/src/modules/kernel.rs | 34 +++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/kernel.rs b/crates/victory-hle/src/modules/kernel.rs index 5b8fb20..4fb8e4b 100644 --- a/crates/victory-hle/src/modules/kernel.rs +++ b/crates/victory-hle/src/modules/kernel.rs @@ -85,7 +85,8 @@ fn title_path(module: *mut c_void) -> Option { /// `GetModuleFileName`'s contract: copy with a NUL; if it does not fit, /// truncate to the buffer, report `ERROR_INSUFFICIENT_BUFFER` and return -/// the buffer size. +/// the buffer size. A name that fits clears the last error, as the host +/// does: titles tell a truncated name from a whole one by it. fn copy_truncated(text: &[T], out: &mut [T]) -> u32 { let Some(room) = out.len().checked_sub(1) else { host::set_last_error(ERROR_INSUFFICIENT_BUFFER); @@ -98,9 +99,12 @@ fn copy_truncated(text: &[T], out: &mut [T]) -> u32 { host::set_last_error(ERROR_INSUFFICIENT_BUFFER); return out.len() as u32; } + host::set_last_error(ERROR_SUCCESS); n as u32 } +const ERROR_SUCCESS: u32 = 0; + const ERROR_INSUFFICIENT_BUFFER: u32 = 122; /// Console-only DLLs we implement load as a pseudo handle (titles load @@ -720,6 +724,34 @@ mod tests { assert_eq!(&small, &[b'C', b':', b'\\', 0]); } + /// A name that fits clears the last error, as the host does: titles tell + /// a truncated name from a whole one by it. + #[cfg(windows)] + #[test] + fn a_module_name_that_fits_clears_the_last_error() { + use windows_sys::Win32::Foundation::{GetLastError, SetLastError}; + // SAFETY: this thread's last error. + let last_error = || unsafe { GetLastError() }; + // SAFETY: sets this thread's last error. + let stale = || unsafe { SetLastError(ERROR_INSUFFICIENT_BUFFER) }; + let base = 0x7200_0000_0000; + runtime::set_image_path(base, std::path::Path::new(r"C:\title\game.exe")); + let module = base as *mut c_void; + let (mut wide, mut narrow) = ([0u16; 512], [0u8; 512]); + stale(); + assert_eq!(GetModuleFileNameW(module, wide.as_mut_ptr(), 512), 17); + assert_eq!(last_error(), 0, "a title image, wide"); + stale(); + assert_eq!(GetModuleFileNameA(module, narrow.as_mut_ptr(), 512), 17); + assert_eq!(last_error(), 0, "a title image, narrow"); + // An exact fit is whole too (17 characters and the NUL). + stale(); + assert_eq!(GetModuleFileNameA(module, narrow.as_mut_ptr(), 18), 17); + assert_eq!(last_error(), 0); + assert_eq!(GetModuleFileNameA(module, narrow.as_mut_ptr(), 17), 17); + assert_eq!(last_error(), ERROR_INSUFFICIENT_BUFFER); + } + /// The title's arguments come right after the program, and the list ends /// in a null pointer; without any there is only the program. #[test]