#1351·xi-editor

[xi-core-lib] Out-of-range crash occurs when using APIs

Author: HeeillWangCreated Sep 24, 2023Updated Sep 24, 2023

Details

I found some out-of-range patterns on xi-core-lib public APIs. All of these cases triggered when external crates use API of xi-core-lib. As this issues looks quite obvious, I just list up the panic location. Please let me know if anyone need reproduce code for other cases.

Case-1

Thread '<unnamed>' panicked at 'slice index starts at 1 but ends at 0', xi-core-lib-0.3.0/src/selection.rs:137
    /// Gets a slice of regions that intersect the given range. Regions that    /// merely touch the range at the edges are also included, so it is the
    /// caller's responsibility to further trim them, in particular to only
    /// display one caret in the upstream/downstream cases.
    ///
    /// Performance note: O(log n).
    pub fn regions_in_range(&self, start: usize, end: usize) -> &[SelRegion] {
        let first = self.search(start);
        let mut last = self.search(end);
        if last < self.regions.len() && self.regions[last].min() <= end {
            last += 1;
        }
        &self.regions[first..last]   // out-of-range!
    }

Case-2

Thread '<unnamed>' panicked at 'index out of bounds: the len is 0 but the index is 0', xi-core-lib-0.3.0/src/selection.rs:73
    /// Collapse all selections into a single caret.
    pub fn collapse(&mut self) {
        self.regions.truncate(1);
        self.regions[0].start = self.regions[0].end;   // out-of-range!
    }

Expected vs Actual

There should be range check via assert statement or should mention panic condition on docs.