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. #90144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
13 changes: 2 additions & 11 deletions library/std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,17 +676,8 @@ if #[cfg(not(target_vendor = "uwp"))] {

pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;

pub const TOKEN_READ: DWORD = 0x20008;

#[link(name = "advapi32")]
extern "system" {
// Allowed but unused by UWP
pub fn OpenProcessToken(
ProcessHandle: HANDLE,
DesiredAccess: DWORD,
TokenHandle: *mut HANDLE,
) -> BOOL;
}
// This value is hardcoded in processthreadsapi.h GetCurrentProcessToken()
pub const CURRENT_PROCESS_TOKEN: HANDLE = (-4isize) as HANDLE;

#[link(name = "userenv")]
extern "system" {
Expand Down
10 changes: 1 addition & 9 deletions library/std/src/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,9 @@ pub fn temp_dir() -> PathBuf {
#[cfg(not(target_vendor = "uwp"))]
fn home_dir_crt() -> Option<PathBuf> {
unsafe {
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);
super::fill_utf16_buf(
|buf, mut sz| {
match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
match c::GetUserProfileDirectoryW(c::CURRENT_PROCESS_TOKEN, buf, &mut sz) {
0 if c::GetLastError() != c::ERROR_INSUFFICIENT_BUFFER => 0,
0 => sz,
_ => sz - 1, // sz includes the null terminator
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ fn make_win_dist(
"libmoldname.a",
"libpthread.a",
//Windows import libs
"libadvapi32.a",
"libbcrypt.a",
"libcomctl32.a",
"libcomdlg32.a",
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make-fulldeps/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ endif
# Extra flags needed to compile a working executable with the standard library
ifdef IS_WINDOWS
ifdef IS_MSVC
EXTRACFLAGS := ws2_32.lib userenv.lib advapi32.lib bcrypt.lib
EXTRACFLAGS := ws2_32.lib userenv.lib bcrypt.lib
else
EXTRACFLAGS := -lws2_32 -luserenv -lbcrypt
EXTRACXXFLAGS := -lstdc++
Expand Down