Skip to content

Commit e59e575

Browse files
authored
Merge pull request #2184 from lzutao/smaller-unsafe
Make unsafe part smaller
2 parents 5da28ea + 5aa4988 commit e59e575

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

src/cli/self_update.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,18 @@ fn delete_rustup_and_cargo_home() -> Result<()> {
897897
// https://stackoverflow.com/questions/10319526/understanding-a-self-deleting-program-in-c
898898
#[cfg(windows)]
899899
fn delete_rustup_and_cargo_home() -> Result<()> {
900+
use std::io;
901+
use std::mem;
902+
use std::os::windows::ffi::OsStrExt;
903+
use std::ptr;
900904
use std::thread;
901905
use std::time::Duration;
906+
use winapi::shared::minwindef::DWORD;
907+
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
908+
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
909+
use winapi::um::minwinbase::SECURITY_ATTRIBUTES;
910+
use winapi::um::winbase::FILE_FLAG_DELETE_ON_CLOSE;
911+
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, GENERIC_READ};
902912

903913
// CARGO_HOME, hopefully empty except for bin/rustup.exe
904914
let cargo_home = utils::cargo_home()?;
@@ -914,32 +924,20 @@ fn delete_rustup_and_cargo_home() -> Result<()> {
914924
// of CARGO_HOME.
915925
let numbah: u32 = rand::random();
916926
let gc_exe = work_path.join(&format!("rustup-gc-{:x}.exe", numbah));
927+
// Copy rustup (probably this process's exe) to the gc exe
928+
utils::copy_file(&rustup_path, &gc_exe)?;
929+
let gc_exe_win: Vec<_> = gc_exe.as_os_str().encode_wide().chain(Some(0)).collect();
930+
931+
// Make the sub-process opened by gc exe inherit its attribute.
932+
let mut sa = SECURITY_ATTRIBUTES {
933+
nLength: mem::size_of::<SECURITY_ATTRIBUTES> as DWORD,
934+
lpSecurityDescriptor: ptr::null_mut(),
935+
bInheritHandle: 1,
936+
};
917937

918-
use std::io;
919-
use std::mem;
920-
use std::os::windows::ffi::OsStrExt;
921-
use std::ptr;
922-
use winapi::shared::minwindef::DWORD;
923-
use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING};
924-
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
925-
use winapi::um::minwinbase::SECURITY_ATTRIBUTES;
926-
use winapi::um::winbase::FILE_FLAG_DELETE_ON_CLOSE;
927-
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, GENERIC_READ};
928-
929-
unsafe {
930-
// Copy rustup (probably this process's exe) to the gc exe
931-
utils::copy_file(&rustup_path, &gc_exe)?;
932-
933-
let mut gc_exe_win: Vec<_> = gc_exe.as_os_str().encode_wide().collect();
934-
gc_exe_win.push(0);
935-
938+
let _g = unsafe {
936939
// Open an inheritable handle to the gc exe marked
937-
// FILE_FLAG_DELETE_ON_CLOSE. This will be inherited
938-
// by subsequent processes.
939-
let mut sa = mem::zeroed::<SECURITY_ATTRIBUTES>();
940-
sa.nLength = mem::size_of::<SECURITY_ATTRIBUTES>() as DWORD;
941-
sa.bInheritHandle = 1;
942-
940+
// FILE_FLAG_DELETE_ON_CLOSE.
943941
let gc_handle = CreateFileW(
944942
gc_exe_win.as_ptr(),
945943
GENERIC_READ,
@@ -955,23 +953,23 @@ fn delete_rustup_and_cargo_home() -> Result<()> {
955953
return Err(err).chain_err(|| ErrorKind::WindowsUninstallMadness);
956954
}
957955

958-
let _g = scopeguard::guard(gc_handle, |h| {
956+
scopeguard::guard(gc_handle, |h| {
959957
let _ = CloseHandle(h);
960-
});
958+
})
959+
};
961960

962-
Command::new(gc_exe)
963-
.spawn()
964-
.chain_err(|| ErrorKind::WindowsUninstallMadness)?;
961+
Command::new(gc_exe)
962+
.spawn()
963+
.chain_err(|| ErrorKind::WindowsUninstallMadness)?;
965964

966-
// The catch 22 article says we must sleep here to give
967-
// Windows a chance to bump the processes file reference
968-
// count. acrichto though is in disbelief and *demanded* that
969-
// we not insert a sleep. If Windows failed to uninstall
970-
// correctly it is because of him.
965+
// The catch 22 article says we must sleep here to give
966+
// Windows a chance to bump the processes file reference
967+
// count. acrichto though is in disbelief and *demanded* that
968+
// we not insert a sleep. If Windows failed to uninstall
969+
// correctly it is because of him.
971970

972-
// (.. and months later acrichto owes me a beer).
973-
thread::sleep(Duration::from_millis(100));
974-
}
971+
// (.. and months later acrichto owes me a beer).
972+
thread::sleep(Duration::from_millis(100));
975973

976974
Ok(())
977975
}
@@ -1027,23 +1025,23 @@ fn wait_for_parent() -> Result<()> {
10271025
return Err(err).chain_err(|| ErrorKind::WindowsUninstallMadness);
10281026
}
10291027

1030-
let _g = scopeguard::guard(snapshot, |h| {
1028+
let snapshot = scopeguard::guard(snapshot, |h| {
10311029
let _ = CloseHandle(h);
10321030
});
10331031

10341032
let mut entry: PROCESSENTRY32 = mem::zeroed();
10351033
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
10361034

10371035
// Iterate over system processes looking for ours
1038-
let success = Process32First(snapshot, &mut entry);
1036+
let success = Process32First(*snapshot, &mut entry);
10391037
if success == 0 {
10401038
let err = io::Error::last_os_error();
10411039
return Err(err).chain_err(|| ErrorKind::WindowsUninstallMadness);
10421040
}
10431041

10441042
let this_pid = GetCurrentProcessId();
10451043
while entry.th32ProcessID != this_pid {
1046-
let success = Process32Next(snapshot, &mut entry);
1044+
let success = Process32Next(*snapshot, &mut entry);
10471045
if success == 0 {
10481046
let err = io::Error::last_os_error();
10491047
return Err(err).chain_err(|| ErrorKind::WindowsUninstallMadness);
@@ -1062,12 +1060,12 @@ fn wait_for_parent() -> Result<()> {
10621060
return Ok(());
10631061
}
10641062

1065-
let _g = scopeguard::guard(parent, |h| {
1063+
let parent = scopeguard::guard(parent, |h| {
10661064
let _ = CloseHandle(h);
10671065
});
10681066

10691067
// Wait for our parent to exit
1070-
let res = WaitForSingleObject(parent, INFINITE);
1068+
let res = WaitForSingleObject(*parent, INFINITE);
10711069

10721070
if res != WAIT_OBJECT_0 {
10731071
let err = io::Error::last_os_error();

0 commit comments

Comments
 (0)