diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-07-15 12:30:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-15 12:30:25 -0400 |
commit | 635eed93731c3616cacf53860b9aeeeb8cfe158b (patch) | |
tree | 04509d5720189fe12b2021500c461ccea1ddfa8e | |
parent | ee0c0586b318fe23908a3b9b4311b26d79c5c8a1 (diff) |
chore: fix Windows specific clippy errors (#15212)
-rw-r--r-- | cli/deno_dir.rs | 1 | ||||
-rw-r--r-- | cli/lsp/parent_process_checker.rs | 1 | ||||
-rw-r--r-- | cli/windows_util.rs | 1 | ||||
-rw-r--r-- | ext/ffi/build.rs | 7 | ||||
-rw-r--r-- | ext/ffi/lib.rs | 32 | ||||
-rw-r--r-- | runtime/ops/process.rs | 17 | ||||
-rw-r--r-- | runtime/ops/runtime.rs | 1 | ||||
-rw-r--r-- | runtime/ops/tty.rs | 3 | ||||
-rw-r--r-- | runtime/permissions.rs | 1 | ||||
-rw-r--r-- | test_util/src/pty.rs | 89 |
10 files changed, 93 insertions, 60 deletions
diff --git a/cli/deno_dir.rs b/cli/deno_dir.rs index 0a1864f2c..8ca705691 100644 --- a/cli/deno_dir.rs +++ b/cli/deno_dir.rs @@ -133,6 +133,7 @@ mod dirs { use winapi::um::{combaseapi, knownfolders, shlobj, shtypes, winbase, winnt}; fn known_folder(folder_id: shtypes::REFKNOWNFOLDERID) -> Option<PathBuf> { + // SAFETY: winapi calls unsafe { let mut path_ptr: winnt::PWSTR = std::ptr::null_mut(); let result = shlobj::SHGetKnownFolderPath( diff --git a/cli/lsp/parent_process_checker.rs b/cli/lsp/parent_process_checker.rs index e4a359bd9..5983d914a 100644 --- a/cli/lsp/parent_process_checker.rs +++ b/cli/lsp/parent_process_checker.rs @@ -39,6 +39,7 @@ fn is_process_active(process_id: u32) -> bool { use winapi::um::synchapi::WaitForSingleObject; use winapi::um::winnt::SYNCHRONIZE; + // SAFETY: winapi calls unsafe { let process = OpenProcess(SYNCHRONIZE, FALSE, process_id as DWORD); let result = if process == NULL { diff --git a/cli/windows_util.rs b/cli/windows_util.rs index deab9f38a..0801ff2f5 100644 --- a/cli/windows_util.rs +++ b/cli/windows_util.rs @@ -5,6 +5,7 @@ /// constructed from a stdio handle; if the handle is null this causes a panic. pub fn ensure_stdio_open() { #[cfg(windows)] + // SAFETY: winapi calls unsafe { use std::mem::size_of; use winapi::shared::minwindef::DWORD; diff --git a/ext/ffi/build.rs b/ext/ffi/build.rs index 091dd9599..1debd6b9c 100644 --- a/ext/ffi/build.rs +++ b/ext/ffi/build.rs @@ -1,8 +1,9 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use std::env; - +#[cfg(not(target_os = "windows"))] fn build_tcc() { + use std::env; + { // TODO(@littledivy): Windows support for fast call. // let tcc_path = root @@ -58,6 +59,8 @@ fn main() {} #[cfg(not(target_os = "windows"))] fn main() { + use std::env; + if let Ok(tcc_path) = env::var("TCC_PATH") { println!("cargo:rustc-link-search=native={}", tcc_path); } else { diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 737ea9db2..ac792de44 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -518,8 +518,10 @@ pub(crate) fn format_error(e: dlopen::Error, path: String) -> String { let arguments = [path.as_ptr()]; loop { - unsafe { - let length = FormatMessageW( + // SAFETY: + // winapi call to format the error message + let length = unsafe { + FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, std::ptr::null_mut(), err_num as DWORD, @@ -527,22 +529,24 @@ pub(crate) fn format_error(e: dlopen::Error, path: String) -> String { buf.as_mut_ptr(), buf.len() as DWORD, arguments.as_ptr() as _, - ); - - if length == 0 { - let err_num = GetLastError(); - if err_num == ERROR_INSUFFICIENT_BUFFER { - buf.resize(buf.len() * 2, 0); - continue; - } + ) + }; - // Something went wrong, just return the original error. - return e.to_string(); + if length == 0 { + // SAFETY: + // winapi call to get the last error message + let err_num = unsafe { GetLastError() }; + if err_num == ERROR_INSUFFICIENT_BUFFER { + buf.resize(buf.len() * 2, 0); + continue; } - let msg = String::from_utf16_lossy(&buf[..length as usize]); - return msg; + // Something went wrong, just return the original error. + return e.to_string(); } + + let msg = String::from_utf16_lossy(&buf[..length as usize]); + return msg; } } _ => e.to_string(), diff --git a/runtime/ops/process.rs b/runtime/ops/process.rs index a48cd122d..e5650e305 100644 --- a/runtime/ops/process.rs +++ b/runtime/ops/process.rs @@ -318,21 +318,26 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> { } else if pid <= 0 { Err(type_error("Invalid pid")) } else { + // SAFETY: winapi call let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) }; if handle.is_null() { + // SAFETY: winapi call let err = match unsafe { GetLastError() } { ERROR_INVALID_PARAMETER => Error::from(NotFound), // Invalid `pid`. errno => Error::from_raw_os_error(errno as i32), }; Err(err.into()) } else { - let is_terminated = unsafe { TerminateProcess(handle, 1) }; - unsafe { CloseHandle(handle) }; - match is_terminated { - FALSE => Err(Error::last_os_error().into()), - TRUE => Ok(()), - _ => unreachable!(), + // SAFETY: winapi calls + unsafe { + let is_terminated = TerminateProcess(handle, 1); + CloseHandle(handle); + match is_terminated { + FALSE => Err(Error::last_os_error().into()), + TRUE => Ok(()), + _ => unreachable!(), + } } } } diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index 31f9d2732..d12dfab96 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -51,6 +51,7 @@ pub fn ppid() -> i64 { CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, TH32CS_SNAPPROCESS, }; + // SAFETY: winapi calls unsafe { // Take a snapshot of system processes, one of which is ours // and contains our parent's pid diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index ab9553025..2018f954d 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -95,6 +95,7 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { return Err(custom_error("ReferenceError", "null handle")); } let mut original_mode: DWORD = 0; + // SAFETY: winapi call if unsafe { consoleapi::GetConsoleMode(handle, &mut original_mode) } == FALSE { @@ -105,6 +106,7 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> { } else { original_mode | RAW_MODE_MASK }; + // SAFETY: winapi call if unsafe { consoleapi::SetConsoleMode(handle, new_mode) } == FALSE { return Err(Error::last_os_error().into()); } @@ -210,6 +212,7 @@ fn op_console_size( use std::os::windows::io::AsRawHandle; let handle = std_file.as_raw_handle(); + // SAFETY: winapi calls unsafe { let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO = std::mem::zeroed(); diff --git a/runtime/permissions.rs b/runtime/permissions.rs index ab8baec89..de44989eb 100644 --- a/runtime/permissions.rs +++ b/runtime/permissions.rs @@ -1912,6 +1912,7 @@ fn permission_prompt(message: &str, name: &str) -> bool { use winapi::um::winuser::MAPVK_VK_TO_VSC; use winapi::um::winuser::VK_RETURN; + // SAFETY: winapi calls unsafe { let stdin = GetStdHandle(STD_INPUT_HANDLE); // emulate an enter key press to clear any line buffered console characters diff --git a/test_util/src/pty.rs b/test_util/src/pty.rs index 9ca02f857..f69fc8b31 100644 --- a/test_util/src/pty.rs +++ b/test_util/src/pty.rs @@ -153,6 +153,8 @@ mod windows { maybe_env_vars: Option<HashMap<String, String>>, ) -> Self { // https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session + // SAFETY: + // Generous use of winapi to create a PTY (thus large unsafe block). unsafe { let mut size: COORD = std::mem::zeroed(); size.X = 800; @@ -238,22 +240,24 @@ mod windows { impl Read for WinPseudoConsole { fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { - unsafe { - loop { - let mut bytes_read = 0; - let success = ReadFile( + loop { + let mut bytes_read = 0; + // SAFETY: + // winapi call + let success = unsafe { + ReadFile( self.stdout_read_handle.as_raw_handle(), buf.as_mut_ptr() as _, buf.len() as u32, &mut bytes_read, ptr::null_mut(), - ); + ) + }; - // ignore zero-byte writes - let is_zero_byte_write = bytes_read == 0 && success == TRUE; - if !is_zero_byte_write { - return Ok(bytes_read as usize); - } + // ignore zero-byte writes + let is_zero_byte_write = bytes_read == 0 && success == TRUE; + if !is_zero_byte_write { + return Ok(bytes_read as usize); } } } @@ -271,17 +275,19 @@ mod windows { impl std::io::Write for WinPseudoConsole { fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> { - unsafe { - let mut bytes_written = 0; - assert_win_success!(WriteFile( + let mut bytes_written = 0; + // SAFETY: + // winapi call + assert_win_success!(unsafe { + WriteFile( self.stdin_write_handle.as_raw_handle(), buffer.as_ptr() as *const _, buffer.len() as u32, &mut bytes_written, ptr::null_mut(), - )); - Ok(bytes_written as usize) - } + ) + }); + Ok(bytes_written as usize) } fn flush(&mut self) -> std::io::Result<()> { @@ -299,10 +305,14 @@ mod windows { } pub fn duplicate(&self) -> WinHandle { - unsafe { - let process_handle = GetCurrentProcess(); - let mut duplicate_handle = ptr::null_mut(); - assert_win_success!(DuplicateHandle( + // SAFETY: + // winapi call + let process_handle = unsafe { GetCurrentProcess() }; + let mut duplicate_handle = ptr::null_mut(); + // SAFETY: + // winapi call + assert_win_success!(unsafe { + DuplicateHandle( process_handle, self.inner, process_handle, @@ -310,10 +320,10 @@ mod windows { 0, 0, DUPLICATE_SAME_ACCESS, - )); + ) + }); - WinHandle::new(duplicate_handle) - } + WinHandle::new(duplicate_handle) } pub fn as_raw_handle(&self) -> HANDLE { @@ -333,8 +343,10 @@ mod windows { impl Drop for WinHandle { fn drop(&mut self) { - unsafe { - if !self.inner.is_null() && self.inner != INVALID_HANDLE_VALUE { + if !self.inner.is_null() && self.inner != INVALID_HANDLE_VALUE { + // SAFETY: + // winapi call + unsafe { winapi::um::handleapi::CloseHandle(self.inner); } } @@ -347,6 +359,8 @@ mod windows { impl ProcThreadAttributeList { pub fn new(console_handle: HPCON) -> Self { + // SAFETY: + // Generous use of unsafe winapi calls to create a ProcThreadAttributeList. unsafe { // discover size required for the list let mut size = 0; @@ -393,24 +407,23 @@ mod windows { impl Drop for ProcThreadAttributeList { fn drop(&mut self) { + // SAFETY: + // winapi call unsafe { DeleteProcThreadAttributeList(self.as_mut_ptr()) }; } } fn create_pipe() -> (WinHandle, WinHandle) { - unsafe { - let mut read_handle = std::ptr::null_mut(); - let mut write_handle = std::ptr::null_mut(); - - assert_win_success!(CreatePipe( - &mut read_handle, - &mut write_handle, - ptr::null_mut(), - 0 - )); - - (WinHandle::new(read_handle), WinHandle::new(write_handle)) - } + let mut read_handle = std::ptr::null_mut(); + let mut write_handle = std::ptr::null_mut(); + + // SAFETY: + // Creating an anonymous pipe with winapi. + assert_win_success!(unsafe { + CreatePipe(&mut read_handle, &mut write_handle, ptr::null_mut(), 0) + }); + + (WinHandle::new(read_handle), WinHandle::new(write_handle)) } fn to_windows_str(str: &str) -> Vec<u16> { |