From a4bfe3e655660d8a4604c1ac2eb5541c040b4072 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Wed, 15 Mar 2023 15:22:24 -0400 Subject: [PATCH] indexmap: use correct index when inserting through entry api When inserting using the entry API and the robin-hood case is hit, we currently return using the incorrect index, which in turn returns a reference to the wrong entry in the map, causing undefined behaviour in the API. --- src/indexmap.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/indexmap.rs b/src/indexmap.rs index e8843db5..3dee9671 100644 --- a/src/indexmap.rs +++ b/src/indexmap.rs @@ -209,12 +209,9 @@ where // robin hood: steal the spot if it's better for us let index = self.entries.len(); unsafe { self.entries.push_unchecked(Bucket { hash, key, value }) }; + Self::insert_phase_2(&mut self.indices, probe, Pos::new(index, hash)); return Insert::Success(Inserted { - index: Self::insert_phase_2( - &mut self.indices, - probe, - Pos::new(index, hash), - ), + index, old_value: None, }); } else if entry_hash == hash && unsafe { self.entries.get_unchecked(i).key == key } @@ -1372,7 +1369,7 @@ mod tests { panic!("Entry found when empty"); } Entry::Vacant(v) => { - v.insert(value).unwrap(); + assert_eq!(value, *v.insert(value).unwrap()); } }; assert_eq!(value, *src.get(&key).unwrap())