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)
This commit is contained in:
Maraco91 authored and trustno1-coder committed 2026-11-27 17:29:36 -08:00
1 parent 4f5ae578c8
commit cda2be6245
1 file changed
+33 -1
+33 -1
View File
@@ -85,7 +85,8 @@ fn title_path(module: *mut c_void) -> Option<String> {
/// `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<T: Copy + Default>(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<T: Copy + Default>(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]