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 f8bb325

Browse files
authoredJul 16, 2024
clean unsafe op in unsafe fn
1 parent b597017 commit f8bb325

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
 

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@ impl Thread {
2828
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
2929
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
3030
let p = Box::into_raw(Box::new(p));
31-
let mut native: libc::pthread_t = mem::zeroed();
32-
let mut attr: libc::pthread_attr_t = mem::zeroed();
33-
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
31+
let mut native: libc::pthread_t = unsafe { mem::zeroed() };
32+
let mut attr: libc::pthread_attr_t = unsafe { mem::zeroed() };
33+
assert_eq!(unsafe { libc::pthread_attr_init(&mut attr) }, 0);
3434
assert_eq!(
35-
libc::pthread_attr_settee(
36-
&mut attr,
37-
libc::TEESMP_THREAD_ATTR_CA_INHERIT,
38-
libc::TEESMP_THREAD_ATTR_TASK_ID_INHERIT,
39-
libc::TEESMP_THREAD_ATTR_HAS_SHADOW,
40-
),
35+
unsafe {
36+
libc::pthread_attr_settee(
37+
&mut attr,
38+
libc::TEESMP_THREAD_ATTR_CA_INHERIT,
39+
libc::TEESMP_THREAD_ATTR_TASK_ID_INHERIT,
40+
libc::TEESMP_THREAD_ATTR_HAS_SHADOW,
41+
)
42+
},
4143
0,
4244
);
4345

4446
let stack_size = cmp::max(stack, min_stack_size(&attr));
4547

46-
match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
48+
match unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) } {
4749
0 => {}
4850
n => {
4951
assert_eq!(n, libc::EINVAL);
@@ -54,20 +56,20 @@ impl Thread {
5456
let page_size = os::page_size();
5557
let stack_size =
5658
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
57-
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
59+
assert_eq!(unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) }, 0);
5860
}
5961
};
6062

6163
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
6264
// Note: if the thread creation fails and this assert fails, then p will
6365
// be leaked. However, an alternative design could cause double-free
6466
// which is clearly worse.
65-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
67+
assert_eq!(unsafe { libc::pthread_attr_destroy(&mut attr) }, 0);
6668

6769
return if ret != 0 {
6870
// The thread failed to start and as a result p was not consumed. Therefore, it is
6971
// safe to reconstruct the box so that it gets deallocated.
70-
drop(Box::from_raw(p));
72+
drop(unsafe { Box::from_raw(p) });
7173
Err(io::Error::from_raw_os_error(ret))
7274
} else {
7375
// The new thread will start running earliest after the next yield.

0 commit comments

Comments
 (0)
Failed to load comments.