Skip to content

Commit

Permalink
indexmap: use correct index when inserting through entry api
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shaunbennett authored and Dirbaio committed Oct 31, 2023
1 parent 8380165 commit a4bfe3e
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit a4bfe3e

Please sign in to comment.