fix(print): handle carriage returns in --attach text output - #364
Open
jchak7 wants to merge 1 commit into
Open
Conversation
The text formatter for --attach output trimmed a trailing newline but not a carriage return, and quoteIfNeeded escaped \n but not \r. A log line ending in \r\n emitted a raw carriage return, which overwrites the line on a terminal. An embedded \r did the same. Trim \r\n and add \r to the set of characters that force quoting. Also adds the first tests for this package, covering quoteIfNeeded and the log formatting path.
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.
While reading the --attach output code I noticed the text formatter can leak raw carriage returns into terminal output.
In formatText, the log message is trimmed with strings.TrimRight(e.Msg, "\n"), which drops a trailing \n but leaves a trailing \r. And quoteIfNeeded forces quoting on " \t\n"=" but not \r. So a log line ending in \r\n prints as msg= followed by a bare \r, and an embedded \r inside a message prints raw too. On a terminal that \r sends the cursor back to the start of the line and the next write overwrites it. CRLF endings are common (Windows apps, plenty of containers), so this turns up in real logs.
The fix is small:
JSON mode already handled this, since json.Marshal escapes control characters, so this only affects the text output.
This package had no tests, so I added the first ones. They cover quoteIfNeeded across the special characters and the log path for trailing CRLF, embedded CR, and a plain message.
I checked each assertion pins the fix: reverting the trim fails only TestFormatTextLogTrimsTrailingCRLF, and reverting the quoteIfNeeded change fails only the carriage-return cases. gofmt is clean.