Skip to content

Commit

Permalink
Merge pull request #106 from Kogia-sima/fix/uninitialized_reference
Browse files Browse the repository at this point in the history
Fix UB caused by uninitialized reference
  • Loading branch information
Gankra authored Jan 8, 2021
2 parents 5adedcd + 649e858 commit 49b3b8c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ impl<K, V> Node<K, V> {
}
}

// drop empty node without dropping its key and value
unsafe fn drop_empty_node<K, V>(the_box: *mut Node<K, V>) {
// Prevent compiler from trying to drop the un-initialized key and values in the node.
let Node { key, value, .. } = *Box::from_raw(the_box);
mem::forget(key);
mem::forget(value);
// Safety:
// In this crate all `Node` is allocated via `Box` or `alloc`, and `Box` uses the
// Global allocator for its allocation,
// (https://doc.rust-lang.org/std/boxed/index.html#memory-layout) so we can safely
// deallocate the pointer to `Node` by calling `dealloc` method
let layout = std::alloc::Layout::new::<Node<K, V>>();
std::alloc::dealloc(the_box as *mut u8, layout);
}

impl<K: Hash + Eq, V> LinkedHashMap<K, V> {
Expand Down

0 comments on commit 49b3b8c

Please sign in to comment.