diff --git a/library/std/src/sys/windows/os.rs b/library/std/src/sys/windows/os.rs index 829dd5eb97ac2..48656eed5665d 100644 --- a/library/std/src/sys/windows/os.rs +++ b/library/std/src/sys/windows/os.rs @@ -320,22 +320,16 @@ pub fn temp_dir() -> PathBuf { #[cfg(not(target_vendor = "uwp"))] fn home_dir_crt() -> Option { - unsafe { - // The magic constant -4 can be used as the token passed to GetUserProfileDirectoryW below - // instead of us having to go through these multiple steps to get a token. However this is - // not implemented on Windows 7, only Windows 8 and up. When we drop support for Windows 7 - // we can simplify this code. See #90144 for details. - use crate::sys::handle::Handle; - - let me = c::GetCurrentProcess(); - let mut token = ptr::null_mut(); - if c::OpenProcessToken(me, c::TOKEN_READ, &mut token) == 0 { - return None; - } - let _handle = Handle::from_raw_handle(token); + // Defined in processthreadsapi.h. + const CURRENT_PROCESS_TOKEN: usize = -4isize as usize; + super::fill_utf16_buf( |buf, mut sz| { - match c::GetUserProfileDirectoryW(token, buf, &mut sz) { + match c::GetUserProfileDirectoryW( + ptr::from_exposed_addr_mut(CURRENT_PROCESS_TOKEN), + buf, + &mut sz, + ) { 0 if api::get_last_error().code != c::ERROR_INSUFFICIENT_BUFFER => 0, 0 => sz, _ => sz - 1, // sz includes the null terminator