From bdb72b32eeb074e3a0b8559d8ccac458237474a3 Mon Sep 17 00:00:00 2001 From: Nathan Lilienthal Date: Mon, 22 Jun 2026 10:16:01 -0400 Subject: [PATCH] Fix overflow on line damage below the viewport rect_for_line computed the rect's y with unsigned arithmetic. When the window is small and the cell is tall, a damaged line can sit below what fits in the viewport, so (line + 1) * cell_height exceeds y_top and the subtraction underflows, panicking in debug builds. The Rect is built from i32, so compute y as a signed subtraction and let it go negative. overdamage already clamps the result back onto the screen. I think there's probably room for improving the representation of a lot of these value in this area, but I'm not opening that can of worms right now. --- alacritty/src/display/damage.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/alacritty/src/display/damage.rs b/alacritty/src/display/damage.rs index 303eef9c..acf9779d 100644 --- a/alacritty/src/display/damage.rs +++ b/alacritty/src/display/damage.rs @@ -228,9 +228,9 @@ impl<'a> RenderDamageIterator<'a> { let size_info = &self.size_info; let y_top = size_info.height() - size_info.padding_y(); let x = size_info.padding_x() + line_damage.left as u32 * size_info.cell_width(); - let y = y_top - (line_damage.line + 1) as u32 * size_info.cell_height(); + let y = y_top as i32 - (line_damage.line + 1) as i32 * size_info.cell_height() as i32; let width = (line_damage.right - line_damage.left + 1) as u32 * size_info.cell_width(); - Rect::new(x as i32, y as i32, width as i32, size_info.cell_height() as i32) + Rect::new(x as i32, y, width as i32, size_info.cell_height() as i32) } // Make sure to damage near cells to include wide chars. @@ -351,6 +351,20 @@ mod tests { assert_eq!(Rect::new(bound * 2 - cell_size, bound * 2 - cell_size / 2, 0, 0), rect); } + #[test] + fn damage_line_outside_viewport() { + // A small window with a large cell size can leave a damaged line below + // what fits in the viewport. Its rect sits off the top of the screen. + let size_info: SizeInfo = SizeInfo::new(100., 100., 10., 40., 0., 0., true).into(); + + // Line 2 wants y = 100 - 3 * 40, which is negative. + let damage = [LineDamageBounds::new(2, 0, 0)]; + let iter = RenderDamageIterator::new(TermDamageIterator::new(&damage, 0), &size_info); + + // The y math must not underflow and panic. + let _: Vec = iter.collect(); + } + #[test] fn add_viewport_damage() { let mut frame_damage = FrameDamage::default();