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 f3bb34b

Browse files
committedJul 15, 2024
Deny more windows unsafe_op_in_unsafe_fn
1 parent eae9451 commit f3bb34b

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed
 

‎std/src/sys/os_str/wtf8.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The underlying OsString/OsStr implementation on Windows is a
22
//! wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
3+
#![deny(unsafe_op_in_unsafe_fn)]
34

45
use crate::borrow::Cow;
56
use crate::collections::TryReserveError;
@@ -71,7 +72,7 @@ impl Buf {
7172

7273
#[inline]
7374
pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self {
74-
Self { inner: Wtf8Buf::from_bytes_unchecked(s) }
75+
unsafe { Self { inner: Wtf8Buf::from_bytes_unchecked(s) } }
7576
}
7677

7778
pub fn with_capacity(capacity: usize) -> Buf {
@@ -190,7 +191,7 @@ impl Slice {
190191

191192
#[inline]
192193
pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice {
193-
mem::transmute(Wtf8::from_bytes_unchecked(s))
194+
unsafe { mem::transmute(Wtf8::from_bytes_unchecked(s)) }
194195
}
195196

196197
#[track_caller]

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

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
2-
31
use crate::alloc::{GlobalAlloc, Layout, System};
42
use crate::ffi::c_void;
53
use crate::ptr;

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

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

43
use crate::os::windows::prelude::*;
@@ -795,10 +794,12 @@ impl<'a> Iterator for DirBuffIter<'a> {
795794
}
796795

797796
unsafe fn from_maybe_unaligned<'a>(p: *const u16, len: usize) -> Cow<'a, [u16]> {
798-
if p.is_aligned() {
799-
Cow::Borrowed(crate::slice::from_raw_parts(p, len))
800-
} else {
801-
Cow::Owned((0..len).map(|i| p.add(i).read_unaligned()).collect())
797+
unsafe {
798+
if p.is_aligned() {
799+
Cow::Borrowed(crate::slice::from_raw_parts(p, len))
800+
} else {
801+
Cow::Owned((0..len).map(|i| p.add(i).read_unaligned()).collect())
802+
}
802803
}
803804
}
804805

@@ -897,7 +898,9 @@ impl IntoRawHandle for File {
897898

898899
impl FromRawHandle for File {
899900
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
900-
Self { handle: FromInner::from_inner(FromRawHandle::from_raw_handle(raw_handle)) }
901+
unsafe {
902+
Self { handle: FromInner::from_inner(FromRawHandle::from_raw_handle(raw_handle)) }
903+
}
901904
}
902905
}
903906

@@ -1427,10 +1430,12 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
14271430
_hDestinationFile: c::HANDLE,
14281431
lpData: *const c_void,
14291432
) -> u32 {
1430-
if dwStreamNumber == 1 {
1431-
*(lpData as *mut i64) = StreamBytesTransferred;
1433+
unsafe {
1434+
if dwStreamNumber == 1 {
1435+
*(lpData as *mut i64) = StreamBytesTransferred;
1436+
}
1437+
c::PROGRESS_CONTINUE
14321438
}
1433-
c::PROGRESS_CONTINUE
14341439
}
14351440
let pfrom = maybe_verbatim(from)?;
14361441
let pto = maybe_verbatim(to)?;

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

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

43
#[cfg(test)]
54
mod tests;
@@ -73,7 +72,7 @@ impl IntoRawHandle for Handle {
7372

7473
impl FromRawHandle for Handle {
7574
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
76-
Self(FromRawHandle::from_raw_handle(raw_handle))
75+
unsafe { Self(FromRawHandle::from_raw_handle(raw_handle)) }
7776
}
7877
}
7978

@@ -142,19 +141,23 @@ impl Handle {
142141
buf: &mut [u8],
143142
overlapped: *mut c::OVERLAPPED,
144143
) -> io::Result<Option<usize>> {
145-
let len = cmp::min(buf.len(), u32::MAX as usize) as u32;
146-
let mut amt = 0;
147-
let res =
148-
cvt(c::ReadFile(self.as_raw_handle(), buf.as_mut_ptr(), len, &mut amt, overlapped));
149-
match res {
150-
Ok(_) => Ok(Some(amt as usize)),
151-
Err(e) => {
152-
if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
153-
Ok(None)
154-
} else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
155-
Ok(Some(0))
156-
} else {
157-
Err(e)
144+
// SAFETY: We have exclusive access to the buffer and it's up to the caller to
145+
// ensure the OVERLAPPED pointer is valid for the lifetime of this function.
146+
unsafe {
147+
let len = cmp::min(buf.len(), u32::MAX as usize) as u32;
148+
let mut amt = 0;
149+
let res =
150+
cvt(c::ReadFile(self.as_raw_handle(), buf.as_mut_ptr(), len, &mut amt, overlapped));
151+
match res {
152+
Ok(_) => Ok(Some(amt as usize)),
153+
Err(e) => {
154+
if e.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
155+
Ok(None)
156+
} else if e.raw_os_error() == Some(c::ERROR_BROKEN_PIPE as i32) {
157+
Ok(Some(0))
158+
} else {
159+
Err(e)
160+
}
158161
}
159162
}
160163
}
@@ -230,20 +233,24 @@ impl Handle {
230233

231234
// The length is clamped at u32::MAX.
232235
let len = cmp::min(len, u32::MAX as usize) as u32;
233-
let status = c::NtReadFile(
234-
self.as_handle(),
235-
ptr::null_mut(),
236-
None,
237-
ptr::null_mut(),
238-
&mut io_status,
239-
buf,
240-
len,
241-
offset.map(|n| n as _).as_ref(),
242-
None,
243-
);
236+
// SAFETY: It's up to the caller to ensure `buf` is writeable up to
237+
// the provided `len`.
238+
let status = unsafe {
239+
c::NtReadFile(
240+
self.as_handle(),
241+
ptr::null_mut(),
242+
None,
243+
ptr::null_mut(),
244+
&mut io_status,
245+
buf,
246+
len,
247+
offset.map(|n| n as _).as_ref(),
248+
None,
249+
)
250+
};
244251

245252
let status = if status == c::STATUS_PENDING {
246-
c::WaitForSingleObject(self.as_raw_handle(), c::INFINITE);
253+
unsafe { c::WaitForSingleObject(self.as_raw_handle(), c::INFINITE) };
247254
io_status.status()
248255
} else {
249256
status
@@ -261,7 +268,7 @@ impl Handle {
261268
status if c::nt_success(status) => Ok(io_status.Information),
262269

263270
status => {
264-
let error = c::RtlNtStatusToDosError(status);
271+
let error = unsafe { c::RtlNtStatusToDosError(status) };
265272
Err(io::Error::from_raw_os_error(error as _))
266273
}
267274
}

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

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

65
#[cfg(test)]
76
mod tests;
@@ -305,15 +304,21 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
305304
}
306305

307306
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
308-
let k = to_u16s(k)?;
309-
let v = to_u16s(v)?;
307+
// SAFETY: We ensure that k and v are null-terminated wide strings.
308+
unsafe {
309+
let k = to_u16s(k)?;
310+
let v = to_u16s(v)?;
310311

311-
cvt(c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr())).map(drop)
312+
cvt(c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr())).map(drop)
313+
}
312314
}
313315

314316
pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> {
315-
let v = to_u16s(n)?;
316-
cvt(c::SetEnvironmentVariableW(v.as_ptr(), ptr::null())).map(drop)
317+
// SAFETY: We ensure that v is a null-terminated wide strings.
318+
unsafe {
319+
let v = to_u16s(n)?;
320+
cvt(c::SetEnvironmentVariableW(v.as_ptr(), ptr::null())).map(drop)
321+
}
317322
}
318323

319324
pub fn temp_dir() -> PathBuf {

0 commit comments

Comments
 (0)
Failed to load comments.