From 561063b5606746936ee64d2f25a8b49c05a8d52e Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Thu, 14 Nov 2019 02:36:54 +0300 Subject: [PATCH] Fix division by zero without any cols or lines The URL check uses a division to wrap column indices across lines, which will cause a runtime error if the size of the terminal is zero columns wide. Since a lot of our logic assumes that we at least have one column and line to work with and our behavior doesn't matter otherwise, this change fixes the terminal dimensions to have space for at least one cell. --- alacritty/src/display.rs | 42 +++++++++++++++--------------- alacritty_terminal/src/term/mod.rs | 7 ----- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/alacritty/src/display.rs b/alacritty/src/display.rs index 53a5f47..5c4c731 100644 --- a/alacritty/src/display.rs +++ b/alacritty/src/display.rs @@ -186,14 +186,15 @@ impl Display { info!("Cell Size: {} x {}", cell_width, cell_height); info!("Padding: {} x {}", padding_x, padding_y); + // Create new size with at least one column and row let size_info = SizeInfo { dpr, - width: viewport_size.width as f32, - height: viewport_size.height as f32, - cell_width: cell_width as f32, - cell_height: cell_height as f32, - padding_x: padding_x as f32, - padding_y: padding_y as f32, + width: (viewport_size.width as f32).max(cell_width + 2. * padding_x), + height: (viewport_size.height as f32).max(cell_height + 2. * padding_y), + cell_width, + cell_height, + padding_x, + padding_y, }; // Update OpenGL projection @@ -302,25 +303,24 @@ impl Display { self.update_glyph_cache(config, font); } - // Update the window dimensions - if let Some(size) = update_pending.dimensions { - self.size_info.width = size.width as f32; - self.size_info.height = size.height as f32; - } - - let dpr = self.size_info.dpr; - let width = self.size_info.width; - let height = self.size_info.height; let cell_width = self.size_info.cell_width; let cell_height = self.size_info.cell_height; // Recalculate padding - let mut padding_x = f32::from(config.window.padding.x) * dpr as f32; - let mut padding_y = f32::from(config.window.padding.y) * dpr as f32; + let mut padding_x = f32::from(config.window.padding.x) * self.size_info.dpr as f32; + let mut padding_y = f32::from(config.window.padding.y) * self.size_info.dpr as f32; + // Update the window dimensions + if let Some(size) = update_pending.dimensions { + // Ensure we have at least one column and row + self.size_info.width = (size.width as f32).max(cell_width + 2. * padding_x); + self.size_info.height = (size.height as f32).max(cell_height + 2. * padding_y); + } + + // Distribute excess padding equally on all sides if config.window.dynamic_padding { - padding_x = dynamic_padding(padding_x, width, cell_width); - padding_y = dynamic_padding(padding_y, height, cell_height); + padding_x = dynamic_padding(padding_x, self.size_info.width, cell_width); + padding_y = dynamic_padding(padding_y, self.size_info.height, cell_height); } self.size_info.padding_x = padding_x.floor() as f32; @@ -494,7 +494,7 @@ fn compute_cell_size(config: &Config, metrics: &font::Metrics) -> (f32, f32) { let offset_x = f64::from(config.font.offset.x); let offset_y = f64::from(config.font.offset.y); ( - f32::max(1., ((metrics.average_advance + offset_x) as f32).floor()), - f32::max(1., ((metrics.line_height + offset_y) as f32).floor()), + ((metrics.average_advance + offset_x) as f32).floor().max(1.), + ((metrics.line_height + offset_y) as f32).floor().max(1.), ) } diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs index 858402a..063897c 100644 --- a/alacritty_terminal/src/term/mod.rs +++ b/alacritty_terminal/src/term/mod.rs @@ -1023,13 +1023,6 @@ impl Term { /// Resize terminal to new dimensions pub fn resize(&mut self, size: &SizeInfo) { - // Bounds check; lots of math assumes width and height are > 0 - if size.width as usize <= 2 * size.padding_x as usize - || size.height as usize <= 2 * size.padding_y as usize - { - return; - } - let old_cols = self.grid.num_cols(); let old_lines = self.grid.num_lines(); let mut num_cols = size.cols();