nomad / crop

🌾 A pretty fast text rope
https://crates.io/crates/crop
MIT License
261 stars 13 forks source link

Infinite loop on `RopeSlice::line_slice` #16

Closed marcospb19 closed 7 months ago

marcospb19 commented 7 months ago

Take this test as example:

#[test]
fn rope_slice_infinite_loop_line_slice_function() {
    use std::panic::catch_unwind;

    use crate::Rope;

    let r = Rope::from("foo\nbar\r\nbaz\nfoobar\n");
    let s = r.byte_slice(2..17);

    assert_eq!(s.line_slice(..1), "o\n");
    assert_eq!(s.line_slice(1..3), "bar\r\nbaz\n");
    assert_eq!(s.line_slice(3..), "foob");
    // assert_eq!(s.line_slice(4..), "");
    catch_unwind(|| s.line_slice(5..)).unwrap_err();
}

The uncommented line runs indefinitely. I expected an empty RopeSlice (or a panic).

marcospb19 commented 7 months ago

This one panics:

let r = Rope::from("a");
let s = r.byte_slice(0..1);
s.line_slice(0..1); // OK
s.line_slice(1..1); // panic: attempt to subtract with overflow