1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

chore: fix Windows specific clippy errors (#15212)

This commit is contained in:
David Sherret 2022-07-15 12:30:25 -04:00 committed by GitHub
parent ee0c0586b3
commit 635eed9373
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 91 additions and 58 deletions

View file

@ -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(

View file

@ -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 {

View file

@ -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;

View file

@ -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 {

View file

@ -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(),

View file

@ -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!(),
}
}
}
}

View file

@ -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

View file

@ -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();

View file

@ -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

View file

@ -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();
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))
}
(WinHandle::new(read_handle), WinHandle::new(write_handle))
}
fn to_windows_str(str: &str) -> Vec<u16> {