Skip to content

Commit a684153

Browse files
committed
Only call pthread_attr_destroy() after getattr_np() succeeds on all libcs
The calling convention of pthread_getattr_np() is to initialize the pthread_attr_t, so _destroy() is only necessary on success (and _init() isn't necessary beforehand). On the other hand, FreeBSD wants the attr_t to be initialized before pthread_attr_get_np(), and therefore it should always be destroyed afterwards.
1 parent a06edda commit a684153

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/std/src/sys/unix/thread.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ pub mod guard {
294294
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
295295
let mut ret = None;
296296
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
297+
#[cfg(target_os = "freebsd")]
297298
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
298299
#[cfg(target_os = "freebsd")]
299300
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
@@ -305,7 +306,7 @@ pub mod guard {
305306
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
306307
ret = Some(stackaddr);
307308
}
308-
if e == 0 || cfg!(not(target_env = "gnu")) {
309+
if e == 0 || cfg!(target_os = "freebsd") {
309310
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
310311
}
311312
ret
@@ -405,6 +406,7 @@ pub mod guard {
405406
pub unsafe fn current() -> Option<Guard> {
406407
let mut ret = None;
407408
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
409+
#[cfg(target_os = "freebsd")]
408410
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
409411
#[cfg(target_os = "freebsd")]
410412
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
@@ -448,7 +450,7 @@ pub mod guard {
448450
Some(stackaddr..stackaddr + guardsize)
449451
};
450452
}
451-
if e == 0 || cfg!(not(target_env = "gnu")) {
453+
if e == 0 || cfg!(target_os = "freebsd") {
452454
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
453455
}
454456
ret

0 commit comments

Comments
 (0)