Skip to content

Commit 04c8a73

Browse files
committed
Fix last bug in RawTable::clone_from_impl
1 parent 408bd9d commit 04c8a73

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/map.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8509,4 +8509,49 @@ mod test_map {
85098509

85108510
map2.clone_from(&map1);
85118511
}
8512+
8513+
#[test]
8514+
#[should_panic = "panic in clone"]
8515+
fn test_clone_from_memory_leaks() {
8516+
use ::alloc::vec::Vec;
8517+
8518+
struct CheckedClone {
8519+
panic_in_clone: bool,
8520+
need_drop: Vec<i32>,
8521+
}
8522+
impl Clone for CheckedClone {
8523+
fn clone(&self) -> Self {
8524+
if self.panic_in_clone {
8525+
panic!("panic in clone")
8526+
}
8527+
Self {
8528+
panic_in_clone: self.panic_in_clone,
8529+
need_drop: self.need_drop.clone(),
8530+
}
8531+
}
8532+
}
8533+
let mut map1 = HashMap::new();
8534+
map1.insert(
8535+
1,
8536+
CheckedClone {
8537+
panic_in_clone: false,
8538+
need_drop: vec![0, 1, 2],
8539+
},
8540+
);
8541+
map1.insert(
8542+
2,
8543+
CheckedClone {
8544+
panic_in_clone: false,
8545+
need_drop: vec![3, 4, 5],
8546+
},
8547+
);
8548+
map1.insert(
8549+
3,
8550+
CheckedClone {
8551+
panic_in_clone: true,
8552+
need_drop: vec![6, 7, 8],
8553+
},
8554+
);
8555+
let _map2 = map1.clone();
8556+
}
85128557
}

src/raw/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,7 @@ impl<T: Clone, A: Allocator + Clone> RawTable<T, A> {
23372337
// to make sure we drop only the elements that have been
23382338
// cloned so far.
23392339
let mut guard = guard((0, &mut *self), |(index, self_)| {
2340-
if Self::DATA_NEEDS_DROP && !self_.is_empty() {
2340+
if Self::DATA_NEEDS_DROP {
23412341
for i in 0..=*index {
23422342
if self_.is_bucket_full(i) {
23432343
self_.bucket(i).drop();

0 commit comments

Comments
 (0)