Skip to content

Commit

Permalink
Rollup merge of rust-lang#79893 - RalfJung:forget-windows, r=oli-obk
Browse files Browse the repository at this point in the history
Windows TLS: ManuallyDrop instead of mem::forget

The Windows TLS implementation still used `mem::forget` instead of `ManuallyDrop`, leading to the usual problem of "using" the `Box` when it should not be used any more.
  • Loading branch information
Dylan-DPC authored Dec 10, 2020
2 parents e28134c + 594b451 commit e72720b
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions library/std/src/sys/windows/thread_local_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem;
use crate::mem::ManuallyDrop;
use crate::ptr;
use crate::sync::atomic::AtomicPtr;
use crate::sync::atomic::Ordering::SeqCst;
Expand Down Expand Up @@ -111,16 +111,13 @@ struct Node {
}

unsafe fn register_dtor(key: Key, dtor: Dtor) {
let mut node = Box::new(Node { key, dtor, next: ptr::null_mut() });
let mut node = ManuallyDrop::new(Box::new(Node { key, dtor, next: ptr::null_mut() }));

let mut head = DTORS.load(SeqCst);
loop {
node.next = head;
match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) {
Ok(_) => {
mem::forget(node);
return;
}
match DTORS.compare_exchange(head, &mut **node, SeqCst, SeqCst) {
Ok(_) => return, // nothing to drop, we successfully added the node to the list
Err(cur) => head = cur,
}
}
Expand Down

0 comments on commit e72720b

Please sign in to comment.