From bb186da400e5c9a17d8451d009b2d9ddebe6a955 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 2 Feb 2018 00:23:30 -0800 Subject: [PATCH] avoid eagerly casting some parsed sequences to usize This helps to avoid unexpected integer overflow --- term/src/csi.rs | 33 +++++++++++++++------------------ term/src/lib.rs | 18 +++++++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/term/src/csi.rs b/term/src/csi.rs index 00bc379c8..768ad18d3 100644 --- a/term/src/csi.rs +++ b/term/src/csi.rs @@ -35,22 +35,22 @@ pub enum CSIAction { SetReverse(bool), SetStrikethrough(bool), SetInvisible(bool), - SetCursorXY(usize, usize), + SetCursorXY(i64, i64), DeltaCursorXY { x: i64, y: i64 }, EraseInLine(LineErase), EraseInDisplay(DisplayErase), SetDecPrivateMode(DecPrivateMode, bool), DeviceStatusReport, ReportCursorPosition, - SetScrollingRegion { top: usize, bottom: usize }, + SetScrollingRegion { top: i64, bottom: i64 }, RequestDeviceAttributes, - DeleteLines(usize), - InsertLines(usize), - LinePositionAbsolute(usize), - LinePositionRelative(isize), + DeleteLines(i64), + InsertLines(i64), + LinePositionAbsolute(i64), + LinePositionRelative(i64), SaveCursor, RestoreCursor, - ScrollLines(isize), + ScrollLines(i64), } /// Constrol Sequence Initiator (CSI) Parser. @@ -349,8 +349,8 @@ impl<'a> CSIParser<'a> { &[top, bottom] => { self.advance_by(2, params); Some(CSIAction::SetScrollingRegion { - top: (top - 1) as usize, - bottom: (bottom - 1) as usize, + top: top - 1, + bottom: bottom - 1, }) } _ => { @@ -388,10 +388,7 @@ impl<'a> Iterator for CSIParser<'a> { ('H', &[], Some(&[])) => Some(CSIAction::SetCursorXY(0, 0)), ('H', &[], Some(&[y, x])) => { // Co-ordinates are 1-based, but we want 0-based - Some(CSIAction::SetCursorXY( - (x.max(1) - 1) as usize, - (y.max(1) - 1) as usize, - )) + Some(CSIAction::SetCursorXY(x.max(1) - 1, y.max(1) - 1)) } // Erase in Display (ED) @@ -409,15 +406,15 @@ impl<'a> Iterator for CSIParser<'a> { // Insert Liness (IL) ('L', &[], Some(&[])) => Some(CSIAction::InsertLines(1)), - ('L', &[], Some(&[n])) => Some(CSIAction::InsertLines(n as usize)), + ('L', &[], Some(&[n])) => Some(CSIAction::InsertLines(n)), // Delete Liness (DL) ('M', &[], Some(&[])) => Some(CSIAction::DeleteLines(1)), - ('M', &[], Some(&[n])) => Some(CSIAction::DeleteLines(n as usize)), + ('M', &[], Some(&[n])) => Some(CSIAction::DeleteLines(n)), // SU: Scroll Up Lines ('S', &[], Some(&[])) => Some(CSIAction::ScrollLines(-1)), - ('S', &[], Some(&[n])) => Some(CSIAction::ScrollLines(-n as isize)), + ('S', &[], Some(&[n])) => Some(CSIAction::ScrollLines(-n)), // HPR - Character position Relative ('a', &[], Some(&[])) => Some(CSIAction::DeltaCursorXY { x: 1, y: 0 }), @@ -430,11 +427,11 @@ impl<'a> Iterator for CSIParser<'a> { // VPA: Line Position Absolute ('d', &[], Some(&[])) => Some(CSIAction::LinePositionAbsolute(0)), - ('d', &[], Some(&[n])) => Some(CSIAction::LinePositionAbsolute(n as usize)), + ('d', &[], Some(&[n])) => Some(CSIAction::LinePositionAbsolute(n)), // VPR: Line Position Relative ('e', &[], Some(&[])) => Some(CSIAction::LinePositionRelative(0)), - ('e', &[], Some(&[n])) => Some(CSIAction::LinePositionRelative(n as isize)), + ('e', &[], Some(&[n])) => Some(CSIAction::LinePositionRelative(n)), ('h', &[b'?'], Some(params)) => self.dec_set_mode(params), ('l', &[b'?'], Some(params)) => self.dec_reset_mode(params), diff --git a/term/src/lib.rs b/term/src/lib.rs index 7fec52331..873a0a0e1 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -927,7 +927,7 @@ impl vte::Perform for TerminalState { self.pen.set_invisible(on); } CSIAction::SetCursorXY(x, y) => { - self.set_cursor_pos(x, y); + self.set_cursor_pos(x as usize, y as usize); } CSIAction::DeltaCursorXY { x, y } => { self.delta_cursor_pos(x, y); @@ -992,8 +992,8 @@ impl vte::Perform for TerminalState { } CSIAction::SetScrollingRegion { top, bottom } => { let rows = self.screen().physical_rows; - self.scroll_top = top.min(rows - 1); - self.scroll_bottom = bottom.min(rows - 1); + self.scroll_top = (top as usize).min(rows - 1); + self.scroll_bottom = (bottom as usize).min(rows - 1); if self.scroll_top > self.scroll_bottom { std::mem::swap(&mut self.scroll_top, &mut self.scroll_bottom); } @@ -1009,9 +1009,9 @@ impl vte::Perform for TerminalState { "execute delete {} lines with scroll up {} {}", n, top, - top + n + top + n as usize ); - self.screen_mut().scroll_up(top, bottom, n); + self.screen_mut().scroll_up(top, bottom, n as usize); } } CSIAction::InsertLines(n) => { @@ -1022,9 +1022,9 @@ impl vte::Perform for TerminalState { "execute insert {} lines with scroll down {} {}", n, top, - top + n + top + n as usize ); - self.screen_mut().scroll_down(top, bottom, n); + self.screen_mut().scroll_down(top, bottom, n as usize); } } CSIAction::SaveCursor => { @@ -1041,12 +1041,12 @@ impl vte::Perform for TerminalState { } CSIAction::LinePositionAbsolute(row) => { let x = self.cursor_x; - self.set_cursor_pos(x, row); + self.set_cursor_pos(x, row as usize); } CSIAction::LinePositionRelative(row) => { let x = self.cursor_x; let y = self.cursor_y; - self.set_cursor_pos(x, (y as isize + row).min(0) as usize); + self.set_cursor_pos(x, (y as i64 + row).min(0) as usize); } CSIAction::ScrollLines(amount) => { if amount > 0 {