Auto merge of #79893 - RalfJung:forget-windows, r=oli-obk

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.
This commit is contained in:
bors 2020-12-11 07:54:35 +00:00
commit a2e29d67c2

View File

@ -1,4 +1,4 @@
use crate::mem; use crate::mem::ManuallyDrop;
use crate::ptr; use crate::ptr;
use crate::sync::atomic::AtomicPtr; use crate::sync::atomic::AtomicPtr;
use crate::sync::atomic::Ordering::SeqCst; use crate::sync::atomic::Ordering::SeqCst;
@ -111,16 +111,13 @@ struct Node {
} }
unsafe fn register_dtor(key: Key, dtor: Dtor) { 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); let mut head = DTORS.load(SeqCst);
loop { loop {
node.next = head; node.next = head;
match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) { match DTORS.compare_exchange(head, &mut **node, SeqCst, SeqCst) {
Ok(_) => { Ok(_) => return, // nothing to drop, we successfully added the node to the list
mem::forget(node);
return;
}
Err(cur) => head = cur, Err(cur) => head = cur,
} }
} }