From 1015f18e7b2fb130398d1c8e49c4c8079900563f Mon Sep 17 00:00:00 2001 From: John Kerl Date: Sun, 25 Aug 2024 17:40:07 -0400 Subject: [PATCH] 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 --- pkg/lib/file_readers.go | 11 +++++++---- test/cases/non-windows/io-compressed-input/0017/cmd | 1 + .../cases/non-windows/io-compressed-input/0017/experr | 0 .../cases/non-windows/io-compressed-input/0017/expout | 3 +++ test/input/whitespace 1.csv | 2 ++ test/input/whitespace 2.csv | 2 ++ 6 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 test/cases/non-windows/io-compressed-input/0017/cmd create mode 100644 test/cases/non-windows/io-compressed-input/0017/experr create mode 100644 test/cases/non-windows/io-compressed-input/0017/expout create mode 100644 test/input/whitespace 1.csv create mode 100644 test/input/whitespace 2.csv diff --git a/pkg/lib/file_readers.go b/pkg/lib/file_readers.go index a348ff900..d4d3f60be 100644 --- a/pkg/lib/file_readers.go +++ b/pkg/lib/file_readers.go @@ -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() diff --git a/test/cases/non-windows/io-compressed-input/0017/cmd b/test/cases/non-windows/io-compressed-input/0017/cmd new file mode 100644 index 000000000..71f61cc5e --- /dev/null +++ b/test/cases/non-windows/io-compressed-input/0017/cmd @@ -0,0 +1 @@ +mlr --csv cat test/input/whitespace*.csv diff --git a/test/cases/non-windows/io-compressed-input/0017/experr b/test/cases/non-windows/io-compressed-input/0017/experr new file mode 100644 index 000000000..e69de29bb diff --git a/test/cases/non-windows/io-compressed-input/0017/expout b/test/cases/non-windows/io-compressed-input/0017/expout new file mode 100644 index 000000000..88700c714 --- /dev/null +++ b/test/cases/non-windows/io-compressed-input/0017/expout @@ -0,0 +1,3 @@ +a,b,c +1,2,3 +4,5,6 diff --git a/test/input/whitespace 1.csv b/test/input/whitespace 1.csv new file mode 100644 index 000000000..bfde6bfa0 --- /dev/null +++ b/test/input/whitespace 1.csv @@ -0,0 +1,2 @@ +a,b,c +1,2,3 diff --git a/test/input/whitespace 2.csv b/test/input/whitespace 2.csv new file mode 100644 index 000000000..a9411aa9d --- /dev/null +++ b/test/input/whitespace 2.csv @@ -0,0 +1,2 @@ +a,b,c +4,5,6