From 3ca920e881f2f78ada27e0ff19a9705bb194e533 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Fri, 2 Aug 2024 05:17:42 +0200 Subject: [PATCH] fix(span): prevent panic on rendering out of y bounds (#1257) --- src/text/span.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/text/span.rs b/src/text/span.rs index ec64c400..78ae474c 100644 --- a/src/text/span.rs +++ b/src/text/span.rs @@ -370,11 +370,15 @@ impl Widget for Span<'_> { impl WidgetRef for Span<'_> { fn render_ref(&self, area: Rect, buf: &mut Buffer) { - let Rect { mut x, y, .. } = area.intersection(buf.area); + let area = area.intersection(buf.area); + if area.is_empty() { + return; + } + let Rect { mut x, y, .. } = area; for (i, grapheme) in self.styled_graphemes(Style::default()).enumerate() { let symbol_width = grapheme.symbol.width(); let next_x = x.saturating_add(symbol_width as u16); - if next_x > area.intersection(buf.area).right() { + if next_x > area.right() { break; } @@ -629,8 +633,11 @@ mod tests { } #[rstest] - fn render_out_of_bounds(mut small_buf: Buffer) { - let out_of_bounds = Rect::new(20, 20, 10, 1); + #[case::x(20, 0)] + #[case::y(0, 20)] + #[case::both(20, 20)] + fn render_out_of_bounds(mut small_buf: Buffer, #[case] x: u16, #[case] y: u16) { + let out_of_bounds = Rect::new(x, y, 10, 1); Span::raw("Hello, World!").render(out_of_bounds, &mut small_buf); assert_eq!(small_buf, Buffer::empty(small_buf.area)); }