From 7f6b7acfeb016cbc47f68e5b2ccafae2395da92e Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Fri, 7 May 2021 20:47:56 +0000 Subject: Fix crash when copying out of bounds selections Fixes #5067. --- alacritty_terminal/src/selection.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'alacritty_terminal/src/selection.rs') diff --git a/alacritty_terminal/src/selection.rs b/alacritty_terminal/src/selection.rs index e1ae7ed0..428b3f0e 100644 --- a/alacritty_terminal/src/selection.rs +++ b/alacritty_terminal/src/selection.rs @@ -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 }); } -- cgit v1.2.3-54-g00ecf