im-rs icon indicating copy to clipboard operation
im-rs copied to clipboard

Possible Incompleteness

Open YichiZhang0613 opened this issue 1 year ago • 0 comments

I noticed a possible panic in im-rs/src/vector/mod.rs. Although the documentation mentioned it will panic if the index is out of bounds, the code does not check whether index is out of bounds before using it as out[index], self[index]. So I think the code should check it before(using get()).

/// Panics if the index is out of bounds.
    ///
    /// Time: O(log n)
    ///
    /// # Examples
    ///
    /// ```
    /// # #[macro_use] extern crate im;
    /// # use im::Vector;
    /// let mut vec = vector![1, 2, 3];
    /// assert_eq!(vector![1, 5, 3], vec.update(1, 5));
    /// ```
    #[must_use]
    pub fn update(&self, index: usize, value: A) -> Self {
        let mut out = self.clone();
        out[index] = value;
        out
    }

    /// Update the value at index `index` in a vector.
    ///
    /// Returns the previous value at the index.
    ///
    /// Panics if the index is out of bounds.
    ///
    /// Time: O(log n)
    #[inline]
    pub fn set(&mut self, index: usize, value: A) -> A {
        replace(&mut self[index], value)
    }

YichiZhang0613 avatar Feb 25 '24 14:02 YichiZhang0613