From 32f15ede7bf557ac1f5f98a697265970c82778d9 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Wed, 2 Aug 2023 12:18:04 -0400 Subject: [PATCH] text: fix additional truncated newline bug This commit fixes another rune accounting bug that the fuzzer discovered. If we shaped a space in order to acquire line metrics, but the space itself was truncated, we would reset the truncated count to zero. This had the side effect of lying to later logic about whether the truncator run was present at the end of the shaped text. Signed-off-by: Chris Waldon --- text/gotext.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/text/gotext.go b/text/gotext.go index f9ff8c6d..6b91eca7 100644 --- a/text/gotext.go +++ b/text/gotext.go @@ -528,6 +528,7 @@ func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document { if hasNewline { txt = txt[:len(txt)-1] } + truncatedNewline := false if hasNewline && len(txt) == 0 { // If we only have a newline, shape a space to get line metrics. ls, truncated = s.shapeAndWrapText(params, []rune{' '}) @@ -535,6 +536,7 @@ func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document { // Our space was truncated. Since our space didn't exist in any meaningful // capacity, ensure the truncated count is zeroed out. truncated = 0 + truncatedNewline = true } else { // We shaped a space to get proper line metrics, but we need to drop // the rune/glyph info since it isn't actually part of the text. @@ -546,7 +548,7 @@ func (s *shaperImpl) LayoutRunes(params Parameters, txt []rune) document { ls, truncated = s.shapeAndWrapText(params, replaceControlCharacters(txt)) } - didTruncate := truncated > 0 || (params.forceTruncate && params.MaxLines == len(ls)) + didTruncate := truncated > 0 || truncatedNewline || (params.forceTruncate && params.MaxLines == len(ls)) if didTruncate && hasNewline { // We've truncated the newline, since it was at the end and we've truncated some amount of runes