Skip to content

Commit d12cb9b

Browse files
Fix memory leak in os impl
1 parent 4d32b9a commit d12cb9b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

library/std/src/sys/thread_local/os.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ impl<T: 'static, const ALIGN: usize> Storage<T, ALIGN> {
148148
return ptr::null();
149149
}
150150

151+
let value = i.and_then(Option::take).unwrap_or_else(f);
152+
151153
// Manually allocate with the requested alignment
152154
let layout = Layout::new::<Value<T>>().align_to(ALIGN).unwrap();
153155
let ptr: *mut Value<T> = (unsafe { crate::alloc::alloc(layout) }).cast();
154156
if ptr.is_null() {
155157
crate::alloc::handle_alloc_error(layout);
156158
}
157159
unsafe {
158-
ptr.write(Value { value: i.and_then(Option::take).unwrap_or_else(f), key });
160+
ptr.write(Value { value, key });
159161
}
160162

161163
// SAFETY:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
thread_local! {
2+
static LOCAL: u64 = panic!();
3+
}
4+
5+
fn main() {
6+
let _ = std::panic::catch_unwind(|| LOCAL.with(|_| {}));
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
thread 'main' ($TID) panicked at tests/pass/thread_local-panic.rs:LL:CC:
3+
explicit panic
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect

0 commit comments

Comments
 (0)