indexical icon indicating copy to clipboard operation
indexical copied to clipboard

Unsound usage of get_unchecked_mut

Open yaokunzhang opened this issue 7 months ago • 0 comments

Hello! In src/map.rs:219, there is a usage of get_unchecked that is only guarded by a debug_assert!. This can lead to undefined behavior in release builds if the index is ever out of bounds. It might be helpful to strengthen the invariant (e.g., with assert! or type-level guarantees). the code are as follows:

/// Returns a mutable reference to a value for a given key if it exists.
#[inline]
pub fn get_mut<M>(&mut self, idx: impl ToIndex<K, M>) -> Option<&mut V> {
    let idx = idx.to_index(&self.domain);
    debug_assert!(self.domain.contains_index(idx));
    unsafe { self.map.raw.get_unchecked_mut(idx.index()).as_mut() }
}

yaokunzhang avatar Jun 09 '25 12:06 yaokunzhang