Do not panic when StdIn is closed (#1867)

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.
This commit is contained in:
Alessandro Di Stefano 2023-11-18 02:20:12 +01:00 committed by GitHub
parent 3d64bdadc1
commit e5cc57bedf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -362,6 +362,12 @@ EOM
assertEquals "$expected" "$X" assertEquals "$expected" "$X"
} }
testBasicClosedStdIn() {
cat >test.yml <<EOL
a: 1
EOL
X=$(./yq e '.a' test.yml <&-)
assertEquals "1" "$X"
}
source ./scripts/shunit2 source ./scripts/shunit2

View File

@ -227,8 +227,11 @@ func maybeFile(str string) bool {
} }
func processStdInArgs(args []string) []string { func processStdInArgs(args []string) []string {
stat, _ := os.Stdin.Stat() stat, err := os.Stdin.Stat()
pipingStdin := (stat.Mode() & os.ModeCharDevice) == 0 if err != nil {
yqlib.GetLogger().Debugf("error getting stdin: %v", err)
}
pipingStdin := stat != nil && (stat.Mode()&os.ModeCharDevice) == 0
// if we've been given a file, don't automatically // if we've been given a file, don't automatically
// read from stdin. // read from stdin.