fix: Ignore zero-width symbol on rendering Paragraph

This fixes out-of-bounds crash on rendering `Paragraph` when zero-width
character is at end of line.

fix #642

Co-authored-by: rhysd <lin90162@yahoo.co.jp>
This commit is contained in:
Orhun Parmaksız 2023-02-15 14:56:54 +02:00 committed by GitHub
parent 85eefe1d8b
commit 9534d533e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -176,6 +176,10 @@ impl<'a> Widget for Paragraph<'a> {
if y >= self.scroll.0 { if y >= self.scroll.0 {
let mut x = get_line_offset(current_line_width, text_area.width, self.alignment); let mut x = get_line_offset(current_line_width, text_area.width, self.alignment);
for StyledGrapheme { symbol, style } in current_line { for StyledGrapheme { symbol, style } in current_line {
let width = symbol.width();
if width == 0 {
continue;
}
buf.get_mut(text_area.left() + x, text_area.top() + y - self.scroll.0) buf.get_mut(text_area.left() + x, text_area.top() + y - self.scroll.0)
.set_symbol(if symbol.is_empty() { .set_symbol(if symbol.is_empty() {
// If the symbol is empty, the last char which rendered last time will // If the symbol is empty, the last char which rendered last time will
@ -185,7 +189,7 @@ impl<'a> Widget for Paragraph<'a> {
symbol symbol
}) })
.set_style(*style); .set_style(*style);
x += symbol.width() as u16; x += width as u16;
} }
} }
y += 1; y += 1;
@ -195,3 +199,16 @@ impl<'a> Widget for Paragraph<'a> {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn zero_width_char_at_end_of_line() {
let line = "foo\0";
let paragraph = Paragraph::new(line);
let mut buf = Buffer::with_lines(vec![line]);
paragraph.render(*buf.area(), &mut buf);
}
}