Skip to content

Commit 1092713

Browse files
committed
Fix memory leak when obtaining the stack bounds of a thread
1 parent 139f422 commit 1092713

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/vm/src/trap/traphandlers.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,16 @@ cfg_if::cfg_if! {
9999
unsafe fn thread_stack() -> (usize, usize) {
100100
let this_thread = libc::pthread_self();
101101
let mut thread_attrs: libc::pthread_attr_t = mem::zeroed();
102-
#[cfg(not(target_os = "freebsd"))]
103-
libc::pthread_getattr_np(this_thread, &mut thread_attrs);
104-
#[cfg(target_os = "freebsd")]
105-
libc::pthread_attr_get_np(this_thread, &mut thread_attrs);
106102
let mut stackaddr: *mut libc::c_void = ptr::null_mut();
107103
let mut stacksize: libc::size_t = 0;
108-
libc::pthread_attr_getstack(&thread_attrs, &mut stackaddr, &mut stacksize);
104+
#[cfg(not(target_os = "freebsd"))]
105+
let ok = libc::pthread_getattr_np(this_thread, &mut thread_attrs);
106+
#[cfg(target_os = "freebsd")]
107+
let ok = libc::pthread_attr_get_np(this_thread, &mut thread_attrs);
108+
if ok == 0 {
109+
libc::pthread_attr_getstack(&thread_attrs, &mut stackaddr, &mut stacksize);
110+
libc::pthread_attr_destroy(&mut thread_attrs);
111+
}
109112
(stackaddr as usize, stacksize)
110113
}
111114

0 commit comments

Comments
 (0)