From a6ed319e1b1391181d08a40e3fb8f8a1412d2222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Fri, 12 Apr 2024 20:47:59 +0200 Subject: [PATCH] Add `unsafe` to two functions with safety invariants --- library/std/src/sys/pal/windows/thread.rs | 26 ++++++++++++----------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/library/std/src/sys/pal/windows/thread.rs b/library/std/src/sys/pal/windows/thread.rs index 34d90a3b4a0dc..70099e0a3b560 100644 --- a/library/std/src/sys/pal/windows/thread.rs +++ b/library/std/src/sys/pal/windows/thread.rs @@ -45,13 +45,11 @@ impl Thread { Err(io::Error::last_os_error()) }; - extern "system" fn thread_start(main: *mut c_void) -> c::DWORD { - unsafe { - // Next, reserve some stack space for if we otherwise run out of stack. - stack_overflow::reserve_stack(); - // Finally, let's run some code. - Box::from_raw(main as *mut Box)(); - } + unsafe extern "system" fn thread_start(main: *mut c_void) -> c::DWORD { + // Next, reserve some stack space for if we otherwise run out of stack. + stack_overflow::reserve_stack(); + // Finally, let's run some code. + Box::from_raw(main as *mut Box)(); 0 } } @@ -59,15 +57,19 @@ impl Thread { pub fn set_name(name: &CStr) { if let Ok(utf8) = name.to_str() { if let Ok(utf16) = to_u16s(utf8) { - Self::set_name_wide(&utf16) + unsafe { + // SAFETY: the vec returned by `to_u16s` ends with a zero value + Self::set_name_wide(&utf16) + } }; }; } - pub fn set_name_wide(name: &[u16]) { - unsafe { - c::SetThreadDescription(c::GetCurrentThread(), name.as_ptr()); - }; + /// # Safety + /// + /// `name` must end with a zero value + pub unsafe fn set_name_wide(name: &[u16]) { + c::SetThreadDescription(c::GetCurrentThread(), name.as_ptr()); } pub fn join(self) {