Skip to content

Commit

Permalink
Fix segfault if pthread_getattr_np fails
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tavianator committed Sep 9, 2020
1 parent 683d1bc commit a06edda
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions library/std/src/sys/unix/thread.rs
Expand Up @@ -305,7 +305,9 @@ pub mod guard {
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
ret = Some(stackaddr);
}
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
if e == 0 || cfg!(not(target_env = "gnu")) {
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
}
ret
}

Expand Down Expand Up @@ -446,7 +448,9 @@ pub mod guard {
Some(stackaddr..stackaddr + guardsize)
};
}
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
if e == 0 || cfg!(not(target_env = "gnu")) {
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
}
ret
}
}
Expand Down

0 comments on commit a06edda

Please sign in to comment.