Repository navigation
fix(diff): escape tabs and newlines in table cells #3772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ package diff | |
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strconv" | ||
| "strings" | ||
| "text/tabwriter" | ||
|
|
||
|
|
@@ -81,7 +82,7 @@ func WriteTable(w io.Writer, result *Result) error { | |
| baseline := renderCell(c.Baseline) | ||
| target := renderCell(c.Target) | ||
| if _, err := fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", | ||
| strings.ToUpper(string(c.Kind)), c.Path, baseline, target); err != nil { | ||
| strings.ToUpper(string(c.Kind)), escapeCell(c.Path), baseline, target); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return wrapTableErr(err) | ||
| } | ||
| } | ||
|
|
@@ -107,7 +108,17 @@ func renderCell(p *string) string { | |
| if *p == "" { | ||
| return `""` | ||
| } | ||
| return *p | ||
| return escapeCell(*p) | ||
| } | ||
|
|
||
| // escapeCell quotes s when it holds a tab, newline, or carriage return, which | ||
| // tabwriter would otherwise read as a column or row break. Sysctl readings | ||
| // such as /proc/sys/fs/file-nr are tab-separated. | ||
| func escapeCell(s string) string { | ||
| if strings.ContainsAny(s, "\t\n\r") { | ||
| return strconv.Quote(s) | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func wrapTableErr(err error) error { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorten the test doc comment.
The comment documents how the sysctl collector stores
/proc/sysfiles. That is another subsystem's behavior. Keep one sentence on what the test checks. The repository guideline says: "Don't document another subsystem's behavior in a declaration's comment".Source: Coding guidelines