Skip to content

Commit c1b1124

Browse files
committed
Add missing unsafe to internal std::thread::Thread creation functions
1 parent 5cc2d7b commit c1b1124

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

std/src/sys/sync/rwlock/queue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl Node {
202202
fn prepare(&mut self) {
203203
// Fall back to creating an unnamed `Thread` handle to allow locking in
204204
// TLS destructors.
205-
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(|| Thread::new(None)));
205+
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(Thread::new_unnamed));
206206
self.completed = AtomicBool::new(false);
207207
}
208208

std/src/thread/mod.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,11 @@ impl Builder {
470470

471471
let stack_size = stack_size.unwrap_or_else(thread::min_stack);
472472

473-
let my_thread = Thread::new(name.map(|name| {
474-
CString::new(name).expect("thread name may not contain interior null bytes")
475-
}));
473+
let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe {
474+
Thread::new(
475+
CString::new(name).expect("thread name may not contain interior null bytes"),
476+
)
477+
});
476478
let their_thread = my_thread.clone();
477479

478480
let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet {
@@ -694,7 +696,7 @@ pub(crate) fn set_current(thread: Thread) {
694696
/// In contrast to the public `current` function, this will not panic if called
695697
/// from inside a TLS destructor.
696698
pub(crate) fn try_current() -> Option<Thread> {
697-
CURRENT.try_with(|current| current.get_or_init(|| Thread::new(None)).clone()).ok()
699+
CURRENT.try_with(|current| current.get_or_init(|| Thread::new_unnamed()).clone()).ok()
698700
}
699701

700702
/// Gets a handle to the thread that invokes it.
@@ -1290,21 +1292,26 @@ pub struct Thread {
12901292
}
12911293

12921294
impl Thread {
1293-
// Used only internally to construct a thread object without spawning
1294-
pub(crate) fn new(name: Option<CString>) -> Thread {
1295-
if let Some(name) = name {
1296-
Self::new_inner(ThreadName::Other(name))
1297-
} else {
1298-
Self::new_inner(ThreadName::Unnamed)
1299-
}
1295+
/// Used only internally to construct a thread object without spawning.
1296+
///
1297+
/// # Safety
1298+
/// `name` must be valid UTF-8.
1299+
pub(crate) unsafe fn new(name: CString) -> Thread {
1300+
unsafe { Self::new_inner(ThreadName::Other(name)) }
1301+
}
1302+
1303+
pub(crate) fn new_unnamed() -> Thread {
1304+
unsafe { Self::new_inner(ThreadName::Unnamed) }
13001305
}
13011306

13021307
// Used in runtime to construct main thread
13031308
pub(crate) fn new_main() -> Thread {
1304-
Self::new_inner(ThreadName::Main)
1309+
unsafe { Self::new_inner(ThreadName::Main) }
13051310
}
13061311

1307-
fn new_inner(name: ThreadName) -> Thread {
1312+
/// # Safety
1313+
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
1314+
unsafe fn new_inner(name: ThreadName) -> Thread {
13081315
// We have to use `unsafe` here to construct the `Parker` in-place,
13091316
// which is required for the UNIX implementation.
13101317
//

0 commit comments

Comments
 (0)