Fix prepipe handling when filenames have whitespace (#1627)

* Fix prepipe handling when filenames have whitespace

* unit-test data

* Windows-only unit-test item

* Fix Windows fails; neaten
This commit is contained in:
John Kerl 2024-08-25 17:40:07 -04:00 committed by GitHub
parent 16a898cff4
commit 1015f18e7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 15 additions and 4 deletions

View file

@ -128,21 +128,24 @@ func openPrepipedHandleForRead(
// Avoids shell-injection cases by replacing single-quote with backslash
// single-quote and double-quote with backslack double-quote, then wrapping the
// entire result in initial and final single-quote.
//
// TODO: test on Windows. Maybe needs move to pkg/platform.
// Also wraps in single quotes in case the filename has whitespace in it
func escapeFileNameForPopen(filename string) string {
var buffer bytes.Buffer
foundQuote := false
foundQuoteOrSpace := false
for _, c := range filename {
if c == '\'' || c == '"' {
buffer.WriteRune('\'')
buffer.WriteRune(c)
buffer.WriteRune('\'')
foundQuoteOrSpace = true
} else if c == ' ' {
buffer.WriteRune(c)
foundQuoteOrSpace = true
} else {
buffer.WriteRune(c)
}
}
if foundQuote {
if foundQuoteOrSpace {
return "'" + buffer.String() + "'"
} else {
return buffer.String()

View file

@ -0,0 +1 @@
mlr --csv cat test/input/whitespace*.csv

View file

@ -0,0 +1,3 @@
a,b,c
1,2,3
4,5,6

View file

@ -0,0 +1,2 @@
a,b,c
1,2,3
1 a b c
2 1 2 3

View file

@ -0,0 +1,2 @@
a,b,c
4,5,6
1 a b c
2 4 5 6