Skip to content

Commit

Permalink
Forbid unsafe_op_in_unsafe_fn in sys/pal/windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Jul 24, 2024
1 parent 9b87fbc commit 7cd25b1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions library/std/src/sys/pal/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,26 @@ compat_fn_with_fallback! {
// >= Win10 1607
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
pub fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL
unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
}

// >= Win10 1607
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreaddescription
pub fn GetThreadDescription(hthread: HANDLE, lpthreaddescription: *mut PWSTR) -> HRESULT {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL
unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
}

// >= Win8 / Server 2012
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
#[cfg(target_vendor = "win7")]
pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () {
GetSystemTimeAsFileTime(lpsystemtimeasfiletime)
unsafe { GetSystemTimeAsFileTime(lpsystemtimeasfiletime) }
}

// >= Win11 / Server 2022
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
pub fn GetTempPath2W(bufferlength: u32, buffer: PWSTR) -> u32 {
GetTempPathW(bufferlength, buffer)
unsafe { GetTempPathW(bufferlength, buffer) }
}
}

Expand Down
14 changes: 9 additions & 5 deletions library/std/src/sys/pal/windows/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ macro_rules! compat_fn_with_fallback {
static PTR: AtomicPtr<c_void> = AtomicPtr::new(load as *mut _);

unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype {
let func = load_from_module(Module::new($module));
func($($argname),*)
unsafe {
let func = load_from_module(Module::new($module));
func($($argname),*)
}
}

fn load_from_module(module: Option<Module>) -> F {
Expand All @@ -182,8 +184,10 @@ macro_rules! compat_fn_with_fallback {

#[inline(always)]
pub unsafe fn call($($argname: $argtype),*) -> $rettype {
let func: F = mem::transmute(PTR.load(Ordering::Relaxed));
func($($argname),*)
unsafe {
let func: F = mem::transmute(PTR.load(Ordering::Relaxed));
func($($argname),*)
}
}
}
#[allow(unused)]
Expand Down Expand Up @@ -225,7 +229,7 @@ macro_rules! compat_fn_optional {
}
#[inline]
pub unsafe extern "system" fn $symbol($($argname: $argtype),*) $(-> $rettype)? {
$symbol::option().unwrap()($($argname),*)
unsafe { $symbol::option().unwrap()($($argname),*) }
}
)+
)
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(missing_docs, nonstandard_style)]
#![deny(unsafe_op_in_unsafe_fn)]
#![forbid(unsafe_op_in_unsafe_fn)]

use crate::ffi::{OsStr, OsString};
use crate::io::ErrorKind;
Expand Down

0 comments on commit 7cd25b1

Please sign in to comment.