From 50fecc7ee7c9493e9f4ab091660fd8a8493fe9d8 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Thu, 7 Mar 2024 17:22:55 +0100 Subject: [PATCH] Fix index calculation in panic guard of clone_from_impl 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. --- crates/rune-alloc/src/hashbrown/raw/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rune-alloc/src/hashbrown/raw/mod.rs b/crates/rune-alloc/src/hashbrown/raw/mod.rs index b7a8053a7..ea85ff38d 100644 --- a/crates/rune-alloc/src/hashbrown/raw/mod.rs +++ b/crates/rune-alloc/src/hashbrown/raw/mod.rs @@ -3165,7 +3165,7 @@ impl RawTable { // 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(); } @@ -3179,7 +3179,7 @@ impl RawTable { 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.