Skip to content

Commit a4bfe3e

Browse files
shaunbennettDirbaio
authored andcommitted
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.
1 parent 8380165 commit a4bfe3e

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/indexmap.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,9 @@ where
209209
// robin hood: steal the spot if it's better for us
210210
let index = self.entries.len();
211211
unsafe { self.entries.push_unchecked(Bucket { hash, key, value }) };
212+
Self::insert_phase_2(&mut self.indices, probe, Pos::new(index, hash));
212213
return Insert::Success(Inserted {
213-
index: Self::insert_phase_2(
214-
&mut self.indices,
215-
probe,
216-
Pos::new(index, hash),
217-
),
214+
index,
218215
old_value: None,
219216
});
220217
} else if entry_hash == hash && unsafe { self.entries.get_unchecked(i).key == key }
@@ -1372,7 +1369,7 @@ mod tests {
13721369
panic!("Entry found when empty");
13731370
}
13741371
Entry::Vacant(v) => {
1375-
v.insert(value).unwrap();
1372+
assert_eq!(value, *v.insert(value).unwrap());
13761373
}
13771374
};
13781375
assert_eq!(value, *src.get(&key).unwrap())

0 commit comments

Comments
 (0)