Skip to content

Commit 7685fcd

Browse files
committed
Auto merge of #407 - JustForFun88:clone_from, r=Amanieu
Fix last bug in `RawTable::clone_from_impl` Actually `self_` is empty since we are setting the `items` **after** the loop.
2 parents 532aa6b + 04c8a73 commit 7685fcd

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
@@ -8521,4 +8521,49 @@ mod test_map {
85218521

85228522
map2.clone_from(&map1);
85238523
}
8524+
8525+
#[test]
8526+
#[should_panic = "panic in clone"]
8527+
fn test_clone_from_memory_leaks() {
8528+
use ::alloc::vec::Vec;
8529+
8530+
struct CheckedClone {
8531+
panic_in_clone: bool,
8532+
need_drop: Vec<i32>,
8533+
}
8534+
impl Clone for CheckedClone {
8535+
fn clone(&self) -> Self {
8536+
if self.panic_in_clone {
8537+
panic!("panic in clone")
8538+
}
8539+
Self {
8540+
panic_in_clone: self.panic_in_clone,
8541+
need_drop: self.need_drop.clone(),
8542+
}
8543+
}
8544+
}
8545+
let mut map1 = HashMap::new();
8546+
map1.insert(
8547+
1,
8548+
CheckedClone {
8549+
panic_in_clone: false,
8550+
need_drop: vec![0, 1, 2],
8551+
},
8552+
);
8553+
map1.insert(
8554+
2,
8555+
CheckedClone {
8556+
panic_in_clone: false,
8557+
need_drop: vec![3, 4, 5],
8558+
},
8559+
);
8560+
map1.insert(
8561+
3,
8562+
CheckedClone {
8563+
panic_in_clone: true,
8564+
need_drop: vec![6, 7, 8],
8565+
},
8566+
);
8567+
let _map2 = map1.clone();
8568+
}
85248569
}

src/raw/mod.rs

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

0 commit comments

Comments
 (0)