Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a hardcoded constant instead of calling OpenProcessToken. #119032

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions library/std/src/sys/pal/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,33 @@ pub fn temp_dir() -> PathBuf {
super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPath2W(sz, buf) }, super::os2path).unwrap()
}

#[cfg(not(target_vendor = "uwp"))]
#[cfg(all(not(target_vendor = "uwp"), not(target_vendor = "win7")))]
fn home_dir_crt() -> Option<PathBuf> {
unsafe {
// Defined in processthreadsapi.h.
const CURRENT_PROCESS_TOKEN: usize = -4_isize as usize;

super::fill_utf16_buf(
|buf, mut sz| {
match c::GetUserProfileDirectoryW(
ptr::invalid_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
}
},
super::os2path,
)
.ok()
}
}

#[cfg(target_vendor = "win7")]
fn home_dir_crt() -> Option<PathBuf> {
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();
Expand Down
Loading