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 eae9451

Browse files
authoredJul 15, 2024
Rollup merge of rust-lang#127750 - ChrisDenton:safe-unsafe-unsafe, r=workingjubilee
Make os/windows and pal/windows default to `#![deny(unsafe_op_in_unsafe_fn)]` This is to prevent regressions in modules that currently pass. I did also fix up a few trivial places where the module contained only one or two simple wrappers. In more complex cases we should try to ensure the `unsafe` blocks are appropriately scoped and have any appropriate safety comments. This does not fix the windows bits of rust-lang#127747 but it should help prevent regressions until that is done and also make it more obvious specifically which modules need attention.
2 parents 6257980 + 1b70afd commit eae9451

File tree

16 files changed

+51
-25
lines changed

16 files changed

+51
-25
lines changed
 

‎std/src/os/windows/io/raw.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ fn stdio_handle(raw: RawHandle) -> RawHandle {
159159
impl FromRawHandle for fs::File {
160160
#[inline]
161161
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
162-
let handle = handle as sys::c::HANDLE;
163-
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
164-
OwnedHandle::from_raw_handle(handle),
165-
)))
162+
unsafe {
163+
let handle = handle as sys::c::HANDLE;
164+
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
165+
OwnedHandle::from_raw_handle(handle),
166+
)))
167+
}
166168
}
167169
}
168170

@@ -260,24 +262,30 @@ impl AsRawSocket for net::UdpSocket {
260262
impl FromRawSocket for net::TcpStream {
261263
#[inline]
262264
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
263-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
264-
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
265+
unsafe {
266+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
267+
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
268+
}
265269
}
266270
}
267271
#[stable(feature = "from_raw_os", since = "1.1.0")]
268272
impl FromRawSocket for net::TcpListener {
269273
#[inline]
270274
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
271-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
272-
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
275+
unsafe {
276+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
277+
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
278+
}
273279
}
274280
}
275281
#[stable(feature = "from_raw_os", since = "1.1.0")]
276282
impl FromRawSocket for net::UdpSocket {
277283
#[inline]
278284
unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
279-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
280-
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
285+
unsafe {
286+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
287+
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
288+
}
281289
}
282290
}
283291

‎std/src/os/windows/io/socket.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl BorrowedSocket<'_> {
7676
#[stable(feature = "io_safety", since = "1.63.0")]
7777
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
7878
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
79-
Self { socket, _phantom: PhantomData }
79+
unsafe { Self { socket, _phantom: PhantomData } }
8080
}
8181
}
8282

@@ -201,8 +201,10 @@ impl IntoRawSocket for OwnedSocket {
201201
impl FromRawSocket for OwnedSocket {
202202
#[inline]
203203
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
204-
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
205-
Self { socket }
204+
unsafe {
205+
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
206+
Self { socket }
207+
}
206208
}
207209
}
208210

‎std/src/os/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
#![stable(feature = "rust1", since = "1.0.0")]
2626
#![doc(cfg(windows))]
27+
#![deny(unsafe_op_in_unsafe_fn)]
2728

2829
pub mod ffi;
2930
pub mod fs;

‎std/src/os/windows/process.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1616
#[stable(feature = "process_extensions", since = "1.2.0")]
1717
impl FromRawHandle for process::Stdio {
1818
unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
19-
let handle = sys::handle::Handle::from_raw_handle(handle as *mut _);
19+
let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) };
2020
let io = sys::process::Stdio::Handle(handle);
2121
process::Stdio::from_inner(io)
2222
}
@@ -407,7 +407,7 @@ impl CommandExt for process::Command {
407407
attribute: usize,
408408
value: T,
409409
) -> &mut process::Command {
410-
self.as_inner_mut().raw_attribute(attribute, value);
410+
unsafe { self.as_inner_mut().raw_attribute(attribute, value) };
411411
self
412412
}
413413
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ pub fn set_file_information_by_handle<T: SetFileInformation>(
227227
info: *const c_void,
228228
size: u32,
229229
) -> Result<(), WinError> {
230-
let result = c::SetFileInformationByHandle(handle, class, info, size);
231-
(result != 0).then_some(()).ok_or_else(get_last_error)
230+
unsafe {
231+
let result = c::SetFileInformationByHandle(handle, class, info, size);
232+
(result != 0).then_some(()).ok_or_else(get_last_error)
233+
}
232234
}
233235
// SAFETY: The `SetFileInformation` trait ensures that this is safe.
234236
unsafe { set_info(handle, T::CLASS, info.as_ptr(), info.size()) }

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![cfg_attr(test, allow(dead_code))]
55
#![unstable(issue = "none", feature = "windows_c")]
66
#![allow(clippy::style)]
7+
#![allow(unsafe_op_in_unsafe_fn)]
78

89
use crate::ffi::CStr;
910
use crate::mem;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ impl Module {
112112
/// (e.g. kernel32 and ntdll).
113113
pub unsafe fn new(name: &CStr) -> Option<Self> {
114114
// SAFETY: A CStr is always null terminated.
115-
let module = c::GetModuleHandleA(name.as_ptr().cast::<u8>());
116-
NonNull::new(module).map(Self)
115+
unsafe {
116+
let module = c::GetModuleHandleA(name.as_ptr().cast::<u8>());
117+
NonNull::new(module).map(Self)
118+
}
117119
}
118120

119121
// Try to get the address of a function.

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(unsafe_op_in_unsafe_fn)]
12
use core::ptr::addr_of;
23

34
use crate::os::windows::prelude::*;

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![unstable(issue = "none", feature = "windows_handle")]
2+
#![allow(unsafe_op_in_unsafe_fn)]
23

34
#[cfg(test)]
45
mod tests;

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(unsafe_op_in_unsafe_fn)]
12
use crate::marker::PhantomData;
23
use crate::mem::size_of;
34
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(missing_docs, nonstandard_style)]
2+
#![deny(unsafe_op_in_unsafe_fn)]
23

34
use crate::ffi::{OsStr, OsString};
45
use crate::io::ErrorKind;
@@ -54,11 +55,13 @@ impl<T> IoResult<T> for Result<T, api::WinError> {
5455
// SAFETY: must be called only once during runtime initialization.
5556
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
5657
pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
57-
stack_overflow::init();
58+
unsafe {
59+
stack_overflow::init();
5860

59-
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
60-
// exists, we have to call it ourselves.
61-
thread::Thread::set_name_wide(wide_str!("main"));
61+
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
62+
// exists, we have to call it ourselves.
63+
thread::Thread::set_name_wide(wide_str!("main"));
64+
}
6265
}
6366

6467
// SAFETY: must be called only once during runtime cleanup.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl Socket {
436436
pub unsafe fn from_raw(raw: c::SOCKET) -> Self {
437437
debug_assert_eq!(mem::size_of::<c::SOCKET>(), mem::size_of::<RawSocket>());
438438
debug_assert_eq!(mem::align_of::<c::SOCKET>(), mem::align_of::<RawSocket>());
439-
Self::from_raw_socket(raw as RawSocket)
439+
unsafe { Self::from_raw_socket(raw as RawSocket) }
440440
}
441441
}
442442

@@ -486,6 +486,6 @@ impl IntoRawSocket for Socket {
486486

487487
impl FromRawSocket for Socket {
488488
unsafe fn from_raw_socket(raw_socket: RawSocket) -> Self {
489-
Self(FromRawSocket::from_raw_socket(raw_socket))
489+
unsafe { Self(FromRawSocket::from_raw_socket(raw_socket)) }
490490
}
491491
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implementation of `std::os` functionality for Windows.
22
33
#![allow(nonstandard_style)]
4+
#![allow(unsafe_op_in_unsafe_fn)]
45

56
#[cfg(test)]
67
mod tests;

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(unsafe_op_in_unsafe_fn)]
12
use crate::os::windows::prelude::*;
23

34
use crate::ffi::OsStr;

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg_attr(test, allow(dead_code))]
2+
#![allow(unsafe_op_in_unsafe_fn)]
23

34
use crate::sys::c;
45
use crate::thread;

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(unsafe_op_in_unsafe_fn)]
12
use crate::ffi::CStr;
23
use crate::io;
34
use crate::num::NonZero;

0 commit comments

Comments
 (0)
Failed to load comments.