Skip to content

Commit

Permalink
Do not panic when StdIn is closed
Browse files Browse the repository at this point in the history
When the shell executing yq has no open stdin, os.Stdin.Stat() return nil and yq fails to continue. This commit fixes a missing verification on the result of os.Stdin.Stat() in the utils.processStdInArgs function and adds an acceptance test to cover this scenario in the future. This bug affects yq since version 4.26.1.
  • Loading branch information
aleskandro committed Nov 17, 2023
1 parent 4b8c850 commit 87e1802
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion acceptance_tests/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ EOM
assertEquals "$expected" "$X"
}


testBasicClosedStdIn() {
cat >test.yml <<EOL
a: 1
EOL
X=$(./yq e '.a' test.yml <&-)
assertEquals "1" "$X"
}

source ./scripts/shunit2
7 changes: 5 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ func maybeFile(str string) bool {
}

func processStdInArgs(args []string) []string {
stat, _ := os.Stdin.Stat()
pipingStdin := (stat.Mode() & os.ModeCharDevice) == 0
stat, err := os.Stdin.Stat()
if err != nil {
yqlib.GetLogger().Debugf("error getting stdin: %v", err)
}
pipingStdin := err == nil && (stat.Mode()&os.ModeCharDevice) == 0

// if we've been given a file, don't automatically
// read from stdin.
Expand Down

0 comments on commit 87e1802

Please sign in to comment.