Skip to content

Commit a06edda

Browse files
committed
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
1 parent 683d1bc commit a06edda

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ pub mod guard {
305305
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
306306
ret = Some(stackaddr);
307307
}
308-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
308+
if e == 0 || cfg!(not(target_env = "gnu")) {
309+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
310+
}
309311
ret
310312
}
311313

@@ -446,7 +448,9 @@ pub mod guard {
446448
Some(stackaddr..stackaddr + guardsize)
447449
};
448450
}
449-
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
451+
if e == 0 || cfg!(not(target_env = "gnu")) {
452+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
453+
}
450454
ret
451455
}
452456
}

0 commit comments

Comments
 (0)