Skip to content

Commit 70f11ee

Browse files
committed
fix by comments
Signed-off-by: xizheyin <[email protected]>
1 parent 604364f commit 70f11ee

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

library/std/src/sys/pal/unix/stack_overflow.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,12 @@ mod imp {
319319
))]
320320
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
321321
let mut ret = None;
322-
let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = if cfg!(target_os = "freebsd") {
323-
let mut attr = mem::MaybeUninit::uninit();
324-
assert_eq!(libc::pthread_attr_init((&raw mut attr) as *mut _), 0);
325-
attr
326-
} else {
327-
mem::MaybeUninit::zeroed()
328-
};
329-
322+
let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
323+
if !cfg!(target_os = "freebsd") {
324+
attr = mem::MaybeUninit::zeroed();
325+
}
326+
#[cfg(target_os = "freebsd")]
327+
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
330328
#[cfg(target_os = "freebsd")]
331329
let e = libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr());
332330
#[cfg(not(target_os = "freebsd"))]
@@ -517,22 +515,20 @@ mod imp {
517515
// FIXME: I am probably not unsafe.
518516
unsafe fn current_guard() -> Option<Range<usize>> {
519517
let mut ret = None;
520-
let attr: mem::MaybeUninit<libc::pthread_attr_t> = if cfg!(target_os = "freebsd") {
521-
let mut attr = mem::MaybeUninit::uninit();
522-
assert_eq!(libc::pthread_attr_init((&raw mut attr) as *mut _), 0);
523-
attr
524-
} else {
525-
mem::MaybeUninit::zeroed()
526-
};
527518

528-
let mut attr = unsafe { attr.assume_init() };
519+
let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
520+
if !cfg!(target_os = "freebsd") {
521+
attr = mem::MaybeUninit::zeroed();
522+
}
523+
#[cfg(target_os = "freebsd")]
524+
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
529525
#[cfg(target_os = "freebsd")]
530-
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
526+
let e = libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr());
531527
#[cfg(not(target_os = "freebsd"))]
532-
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
528+
let e = libc::pthread_getattr_np(libc::pthread_self(), attr.as_mut_ptr());
533529
if e == 0 {
534530
let mut guardsize = 0;
535-
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
531+
assert_eq!(libc::pthread_attr_getguardsize(attr.as_ptr(), &mut guardsize), 0);
536532
if guardsize == 0 {
537533
if cfg!(all(target_os = "linux", target_env = "musl")) {
538534
// musl versions before 1.1.19 always reported guard
@@ -545,7 +541,7 @@ mod imp {
545541
}
546542
let mut stackptr = crate::ptr::null_mut::<libc::c_void>();
547543
let mut size = 0;
548-
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackptr, &mut size), 0);
544+
assert_eq!(libc::pthread_attr_getstack(attr.as_ptr(), &mut stackptr, &mut size), 0);
549545

550546
let stackaddr = stackptr.addr();
551547
ret = if cfg!(any(target_os = "freebsd", target_os = "netbsd", target_os = "hurd")) {
@@ -566,7 +562,7 @@ mod imp {
566562
};
567563
}
568564
if e == 0 || cfg!(target_os = "freebsd") {
569-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
565+
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
570566
}
571567
ret
572568
}

0 commit comments

Comments
 (0)