Skip to content

Commit d4f73be

Browse files
authored
Rollup merge of rust-lang#76521 - tavianator:fix-pthread-getattr-destroy, r=Amanieu
Fix segfault if pthread_getattr_np fails glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np() fails. Destroying it again leads to a segfault. Fix it by only destroying it on success for glibc. [1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
2 parents d125cba + a684153 commit d4f73be

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

+8-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,9 @@ pub mod guard {
305306
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
306307
ret = Some(stackaddr);
307308
}
308-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
309+
if e == 0 || cfg!(target_os = "freebsd") {
310+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
311+
}
309312
ret
310313
}
311314

@@ -403,6 +406,7 @@ pub mod guard {
403406
pub unsafe fn current() -> Option<Guard> {
404407
let mut ret = None;
405408
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
409+
#[cfg(target_os = "freebsd")]
406410
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
407411
#[cfg(target_os = "freebsd")]
408412
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
@@ -446,7 +450,9 @@ pub mod guard {
446450
Some(stackaddr..stackaddr + guardsize)
447451
};
448452
}
449-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
453+
if e == 0 || cfg!(target_os = "freebsd") {
454+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
455+
}
450456
ret
451457
}
452458
}

0 commit comments

Comments
 (0)