Skip to content

Commit

Permalink
Fixed possible memory leak in secondary::VacantEntry::insert.
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored May 30, 2021
1 parent 1f7d05e commit bd32b7d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,9 +1281,12 @@ impl<'a, K: Key, V> VacantEntry<'a, K, V> {
/// ```
pub fn insert(self, value: V) -> &'a mut V {
let slot = unsafe { self.map.slots.get_unchecked_mut(self.kd.idx as usize) };
self.map.num_elems += 1;
let old_slot = replace(slot, Slot::new_occupied(self.kd.version.get(), value));
core::mem::forget(old_slot); // No need to check, it was vacant.
// Despite the slot being considered Vacant for this entry, it might be occupied
// with an outdated element.
match replace(slot, Slot::new_occupied(self.kd.version.get(), value)) {
Occupied { .. } => {},
Vacant => self.map.num_elems += 1,
}
unsafe { slot.get_unchecked_mut() }
}
}
Expand Down

0 comments on commit bd32b7d

Please sign in to comment.