Skip to content

Commit

Permalink
Fix index calculation in panic guard of clone_from_impl
Browse files Browse the repository at this point in the history
Previously, it was possible for an uninitialized element to be dropped
if all of the following occurred:
- `clone_from` was called where `T: !Copy`.
- The `clone` implementation of `T` panicked.
- The first bucket of the source `HashMap` contained an entry.
  • Loading branch information
Amanieu authored and udoprog committed Mar 7, 2024
1 parent 1fd8968 commit 50fecc7
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/rune-alloc/src/hashbrown/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ impl<T: TryClone, A: Allocator + Clone> RawTable<T, A> {
// cloned so far.
let mut guard = guard((0, &mut *self), |(index, self_)| {
if T::NEEDS_DROP {
for i in 0..=*index {
for i in 0..*index {
if self_.is_bucket_full(i) {
self_.bucket(i).drop();
}
Expand All @@ -3179,7 +3179,7 @@ impl<T: TryClone, A: Allocator + Clone> RawTable<T, A> {
to.write(from.as_ref().try_clone()?);

// Update the index in case we need to unwind.
guard.0 = index;
guard.0 = index + 1;
}

// Successfully cloned all items, no need to clean up.
Expand Down

0 comments on commit 50fecc7

Please sign in to comment.