mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
chore: fix Windows specific clippy errors (#15212)
This commit is contained in:
parent
ee0c0586b3
commit
635eed9373
10 changed files with 91 additions and 58 deletions
|
@ -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(
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn build_tcc() {
|
||||
use std::env;
|
||||
|
||||
fn build_tcc() {
|
||||
{
|
||||
// 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 {
|
||||
|
|
|
@ -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,10 +529,13 @@ 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();
|
||||
// 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;
|
||||
|
@ -544,7 +549,6 @@ pub(crate) fn format_error(e: dlopen::Error, path: String) -> String {
|
|||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => e.to_string(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -318,17 +318,21 @@ 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) };
|
||||
// SAFETY: winapi calls
|
||||
unsafe {
|
||||
let is_terminated = TerminateProcess(handle, 1);
|
||||
CloseHandle(handle);
|
||||
match is_terminated {
|
||||
FALSE => Err(Error::last_os_error().into()),
|
||||
TRUE => Ok(()),
|
||||
|
@ -337,6 +341,7 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_kill(
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,16 +240,19 @@ 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(
|
||||
// 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;
|
||||
|
@ -257,7 +262,6 @@ mod windows {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Pty for WinPseudoConsole {
|
||||
fn write_text(&mut self, text: &str) {
|
||||
|
@ -271,18 +275,20 @@ 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(
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
|
@ -299,10 +305,14 @@ mod windows {
|
|||
}
|
||||
|
||||
pub fn duplicate(&self) -> WinHandle {
|
||||
unsafe {
|
||||
let process_handle = GetCurrentProcess();
|
||||
// SAFETY:
|
||||
// winapi call
|
||||
let process_handle = unsafe { GetCurrentProcess() };
|
||||
let mut duplicate_handle = ptr::null_mut();
|
||||
assert_win_success!(DuplicateHandle(
|
||||
// SAFETY:
|
||||
// winapi call
|
||||
assert_win_success!(unsafe {
|
||||
DuplicateHandle(
|
||||
process_handle,
|
||||
self.inner,
|
||||
process_handle,
|
||||
|
@ -310,11 +320,11 @@ mod windows {
|
|||
0,
|
||||
0,
|
||||
DUPLICATE_SAME_ACCESS,
|
||||
));
|
||||
)
|
||||
});
|
||||
|
||||
WinHandle::new(duplicate_handle)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_raw_handle(&self) -> HANDLE {
|
||||
self.inner
|
||||
|
@ -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 {
|
||||
// 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,25 +407,24 @@ 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
|
||||
));
|
||||
// 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> {
|
||||
use std::os::windows::prelude::OsStrExt;
|
||||
|
|
Loading…
Reference in a new issue