From a13617407e1c4f86a94bb77310dce216838415f3 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 16 Jul 2021 20:56:22 +1000 Subject: [PATCH] Fixed EOF bug when processing empty files --- examples/empty.yaml | 2 -- pkg/yqlib/utils.go | 11 ++++++++++- scripts/acceptance.sh | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/examples/empty.yaml b/examples/empty.yaml index 33fe0314..e69de29b 100644 --- a/examples/empty.yaml +++ b/examples/empty.yaml @@ -1,2 +0,0 @@ -# a: apple -# b: cat diff --git a/pkg/yqlib/utils.go b/pkg/yqlib/utils.go index a54e90a1..718e3a02 100644 --- a/pkg/yqlib/utils.go +++ b/pkg/yqlib/utils.go @@ -15,6 +15,12 @@ func readStream(filename string) (io.Reader, bool, error) { reader := bufio.NewReader(os.Stdin) seperatorBytes, err := reader.Peek(3) + + if err == io.EOF { + // EOF are handled else where.. + return reader, false, nil + } + return reader, string(seperatorBytes) == "---", err } else { // ignore CWE-22 gosec issue - that's more targetted for http based apps that run in a public directory, @@ -25,7 +31,10 @@ func readStream(filename string) (io.Reader, bool, error) { } seperatorBytes := make([]byte, 3) _, err = reader.Read(seperatorBytes) - if err != nil { + if err == io.EOF { + // EOF are handled else where.. + return reader, false, nil + } else if err != nil { return nil, false, err } _, err = reader.Seek(0, 0) diff --git a/scripts/acceptance.sh b/scripts/acceptance.sh index b4935c1f..7564454b 100755 --- a/scripts/acceptance.sh +++ b/scripts/acceptance.sh @@ -113,6 +113,20 @@ if [[ $X != $expected ]]; then exit 1 fi +# handle empty files +./yq e '.' examples/empty.yaml +if [[ $? != 0 ]]; then + echo "Expected no error when processing empty file but got one" + exit 1 +fi + +cat examples/empty.yaml | ./yq e '.' - +if [[ $? != 0 ]]; then + echo "Expected no error when processing empty stdin but got one" + exit 1 +fi + + echo "--success" set -e