Skip to content

Commit e72720b

Browse files
authored
Rollup merge of rust-lang#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.
2 parents e28134c + 594b451 commit e72720b

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

library/std/src/sys/windows/thread_local_key.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::mem;
1+
use crate::mem::ManuallyDrop;
22
use crate::ptr;
33
use crate::sync::atomic::AtomicPtr;
44
use crate::sync::atomic::Ordering::SeqCst;
@@ -111,16 +111,13 @@ struct Node {
111111
}
112112

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

116116
let mut head = DTORS.load(SeqCst);
117117
loop {
118118
node.next = head;
119-
match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) {
120-
Ok(_) => {
121-
mem::forget(node);
122-
return;
123-
}
119+
match DTORS.compare_exchange(head, &mut **node, SeqCst, SeqCst) {
120+
Ok(_) => return, // nothing to drop, we successfully added the node to the list
124121
Err(cur) => head = cur,
125122
}
126123
}

0 commit comments

Comments
 (0)