Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 94b381d

Browse files
committedJul 15, 2024
Some Windows functions are safe
1 parent f3bb34b commit 94b381d

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed
 

‎std/src/sys/pal/windows/io.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(unsafe_op_in_unsafe_fn)]
21
use crate::marker::PhantomData;
32
use crate::mem::size_of;
43
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
@@ -81,19 +80,17 @@ impl<'a> IoSliceMut<'a> {
8180
}
8281

8382
pub fn is_terminal(h: &impl AsHandle) -> bool {
84-
unsafe { handle_is_console(h.as_handle()) }
83+
handle_is_console(h.as_handle())
8584
}
8685

87-
unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
88-
let handle = handle.as_raw_handle();
89-
86+
fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
9087
// A null handle means the process has no console.
91-
if handle.is_null() {
88+
if handle.as_raw_handle().is_null() {
9289
return false;
9390
}
9491

9592
let mut out = 0;
96-
if c::GetConsoleMode(handle, &mut out) != 0 {
93+
if unsafe { c::GetConsoleMode(handle.as_raw_handle(), &mut out) != 0 } {
9794
// False positives aren't possible. If we got a console then we definitely have a console.
9895
return true;
9996
}
@@ -102,9 +99,9 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
10299
msys_tty_on(handle)
103100
}
104101

105-
unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
102+
fn msys_tty_on(handle: BorrowedHandle<'_>) -> bool {
106103
// Early return if the handle is not a pipe.
107-
if c::GetFileType(handle) != c::FILE_TYPE_PIPE {
104+
if unsafe { c::GetFileType(handle.as_raw_handle()) != c::FILE_TYPE_PIPE } {
108105
return false;
109106
}
110107

@@ -120,12 +117,14 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
120117
}
121118
let mut name_info = FILE_NAME_INFO { FileNameLength: 0, FileName: [0; c::MAX_PATH as usize] };
122119
// Safety: buffer length is fixed.
123-
let res = c::GetFileInformationByHandleEx(
124-
handle,
125-
c::FileNameInfo,
126-
core::ptr::addr_of_mut!(name_info) as *mut c_void,
127-
size_of::<FILE_NAME_INFO>() as u32,
128-
);
120+
let res = unsafe {
121+
c::GetFileInformationByHandleEx(
122+
handle.as_raw_handle(),
123+
c::FileNameInfo,
124+
core::ptr::addr_of_mut!(name_info) as *mut c_void,
125+
size_of::<FILE_NAME_INFO>() as u32,
126+
)
127+
};
129128
if res == 0 {
130129
return false;
131130
}
+11-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#![cfg_attr(test, allow(dead_code))]
2-
#![allow(unsafe_op_in_unsafe_fn)]
32

43
use crate::sys::c;
54
use crate::thread;
65

76
/// Reserve stack space for use in stack overflow exceptions.
8-
pub unsafe fn reserve_stack() {
9-
let result = c::SetThreadStackGuarantee(&mut 0x5000);
7+
pub fn reserve_stack() {
8+
let result = unsafe { c::SetThreadStackGuarantee(&mut 0x5000) };
109
// Reserving stack space is not critical so we allow it to fail in the released build of libstd.
1110
// We still use debug assert here so that CI will test that we haven't made a mistake calling the function.
1211
debug_assert_ne!(result, 0, "failed to reserve stack space for exception handling");
1312
}
1413

1514
unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -> i32 {
15+
// SAFETY: It's up to the caller (which in this case is the OS) to ensure that `ExceptionInfo` is valid.
1616
unsafe {
1717
let rec = &(*(*ExceptionInfo).ExceptionRecord);
1818
let code = rec.ExceptionCode;
@@ -27,11 +27,14 @@ unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POIN
2727
}
2828
}
2929

30-
pub unsafe fn init() {
31-
let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
32-
// Similar to the above, adding the stack overflow handler is allowed to fail
33-
// but a debug assert is used so CI will still test that it normally works.
34-
debug_assert!(!result.is_null(), "failed to install exception handler");
30+
pub fn init() {
31+
// SAFETY: `vectored_handler` has the correct ABI and is safe to call during exception handling.
32+
unsafe {
33+
let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
34+
// Similar to the above, adding the stack overflow handler is allowed to fail
35+
// but a debug assert is used so CI will still test that it normally works.
36+
debug_assert!(!result.is_null(), "failed to install exception handler");
37+
}
3538
// Set the thread stack guarantee for the main thread.
3639
reserve_stack();
3740
}

0 commit comments

Comments
 (0)
Failed to load comments.