fix(ansi): reset parser state after grapheme cluster in Truncate#906
Open
sueun-dev wants to merge 1 commit into
Open
fix(ansi): reset parser state after grapheme cluster in Truncate#906sueun-dev wants to merge 1 commit into
sueun-dev wants to merge 1 commit into
Conversation
Truncate could return a string wider than the requested length. In the Utf8State branch of truncate(), pstate was only reset to GroundState on the path that writes the cluster; the ignoring and too-wide continue paths left it mid-sequence. When a grapheme cluster interrupts an unterminated escape (e.g. "\x1b[3" then a multibyte rune), pstate stayed in CSI state, so the next byte was treated as the sequence final byte and written through the default branch past the truncation point. A grapheme cluster always returns the parser to the ground state, so move the reset to cover all exit paths. Adds TestTruncateObeysWidth. For charmbracelet#541
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the
Truncatepart of #541.Truncatecould return a string wider than the requested length. In theUtf8Statebranch oftruncate(),pstatewas reset toGroundStateonly on the path that writes the cluster; the "ignoring" and "too wide"continuepaths left it mid-sequence. When a grapheme cluster interrupts an unterminated escape (e.g.\x1b[3followed by‘),pstatestayed in CSI state, so the next byte was treated as the sequence's final byte and written through thedefaultbranch past the truncation point.A grapheme cluster always returns the parser to the ground state, so this moves the reset to cover all three exit paths.
Before the fix:
The fuzz target in the issue failed almost immediately before; with this change
StringWidth(Truncate(s, n, tail)) <= nholds across a long fuzz run. AddedTestTruncateObeysWidthfor the case.This only touches
Truncate(andTruncateWc, which sharestruncate()). I leftTruncateLeftalone: as noted in the thread it removes n columns from the left, so its result is intentionallytotal-nwide and the issue's fuzz assertion checks a different invariant for it.