Skip to content

Commit 5f21609

Browse files
authored
Rollup merge of #119032 - smmalis37:patch-1, r=ChrisDenton
Use a hardcoded constant instead of calling OpenProcessToken. Now that Win 7 support is dropped, we can resurrect #90144. GetCurrentProcessToken is defined in processthreadsapi.h as: FORCEINLINE HANDLE GetCurrentProcessToken ( VOID ) { return (HANDLE)(LONG_PTR) -4; } Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
2 parents dfdbe30 + 3b63ede commit 5f21609

File tree

1 file changed

+25
-5
lines changed
  • library/std/src/sys/pal/windows

1 file changed

+25
-5
lines changed

library/std/src/sys/pal/windows/os.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,33 @@ pub fn temp_dir() -> PathBuf {
318318
super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPath2W(sz, buf) }, super::os2path).unwrap()
319319
}
320320

321-
#[cfg(not(target_vendor = "uwp"))]
321+
#[cfg(all(not(target_vendor = "uwp"), not(target_vendor = "win7")))]
322+
fn home_dir_crt() -> Option<PathBuf> {
323+
unsafe {
324+
// Defined in processthreadsapi.h.
325+
const CURRENT_PROCESS_TOKEN: usize = -4_isize as usize;
326+
327+
super::fill_utf16_buf(
328+
|buf, mut sz| {
329+
match c::GetUserProfileDirectoryW(
330+
ptr::invalid_mut(CURRENT_PROCESS_TOKEN),
331+
buf,
332+
&mut sz,
333+
) {
334+
0 if api::get_last_error().code != c::ERROR_INSUFFICIENT_BUFFER => 0,
335+
0 => sz,
336+
_ => sz - 1, // sz includes the null terminator
337+
}
338+
},
339+
super::os2path,
340+
)
341+
.ok()
342+
}
343+
}
344+
345+
#[cfg(target_vendor = "win7")]
322346
fn home_dir_crt() -> Option<PathBuf> {
323347
unsafe {
324-
// The magic constant -4 can be used as the token passed to GetUserProfileDirectoryW below
325-
// instead of us having to go through these multiple steps to get a token. However this is
326-
// not implemented on Windows 7, only Windows 8 and up. When we drop support for Windows 7
327-
// we can simplify this code. See #90144 for details.
328348
use crate::sys::handle::Handle;
329349

330350
let me = c::GetCurrentProcess();

0 commit comments

Comments
 (0)