Fix crash when copying out of bounds selections

Fixes #5067.
This commit is contained in:
Christian Duerr 2021-05-07 20:47:56 +00:00 committed by GitHub
parent 8f4b752a98
commit 7f6b7acfeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 10 deletions

View File

@ -11,7 +11,7 @@ use std::ops::{Bound, Range, RangeBounds};
use crate::ansi::CursorShape;
use crate::grid::{Dimensions, GridCell, Indexed};
use crate::index::{Column, Line, Point, Side};
use crate::index::{Boundary, Column, Line, Point, Side};
use crate::term::cell::{Cell, Flags};
use crate::term::{RenderableCursor, Term};
@ -274,6 +274,12 @@ impl Selection {
mem::swap(&mut start, &mut end);
}
// Clamp selection to within grid boundaries.
if end.point.line < term.topmost_line() {
return None;
}
start.point = start.point.grid_clamp(term, Boundary::Grid);
match self.ty {
SelectionType::Simple => self.range_simple(start, end, columns),
SelectionType::Block => self.range_block(start, end),
@ -507,11 +513,11 @@ mod tests {
let mut selection =
Selection::new(SelectionType::Lines, Point::new(Line(9), Column(1)), Side::Left);
selection.update(Point::new(Line(4), Column(1)), Side::Right);
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 7).unwrap();
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 4).unwrap();
assert_eq!(selection.to_range(&term(size.0, size.1)).unwrap(), SelectionRange {
start: Point::new(Line(-3), Column(0)),
end: Point::new(Line(2), Column(4)),
start: Point::new(Line(0), Column(0)),
end: Point::new(Line(5), Column(4)),
is_block: false,
});
}
@ -537,11 +543,11 @@ mod tests {
let mut selection =
Selection::new(SelectionType::Simple, Point::new(Line(9), Column(3)), Side::Right);
selection.update(Point::new(Line(4), Column(1)), Side::Right);
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 7).unwrap();
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 4).unwrap();
assert_eq!(selection.to_range(&term(size.0, size.1)).unwrap(), SelectionRange {
start: Point::new(Line(-3), Column(2)),
end: Point::new(Line(2), Column(3)),
start: Point::new(Line(0), Column(2)),
end: Point::new(Line(5), Column(3)),
is_block: false,
});
}
@ -552,11 +558,11 @@ mod tests {
let mut selection =
Selection::new(SelectionType::Block, Point::new(Line(9), Column(3)), Side::Right);
selection.update(Point::new(Line(4), Column(1)), Side::Right);
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 7).unwrap();
selection = selection.rotate(&size, &(Line(0)..Line(size.0 as i32)), 4).unwrap();
assert_eq!(selection.to_range(&term(size.0, size.1)).unwrap(), SelectionRange {
start: Point::new(Line(-3), Column(2)),
end: Point::new(Line(2), Column(3)),
start: Point::new(Line(0), Column(2)),
end: Point::new(Line(5), Column(3)),
is_block: true
});
}