Skip to content

Commit e5cc57b

Browse files
authored
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.
1 parent 3d64bda commit e5cc57b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

acceptance_tests/basic.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ EOM
362362
assertEquals "$expected" "$X"
363363
}
364364

365-
365+
testBasicClosedStdIn() {
366+
cat >test.yml <<EOL
367+
a: 1
368+
EOL
369+
X=$(./yq e '.a' test.yml <&-)
370+
assertEquals "1" "$X"
371+
}
366372

367373
source ./scripts/shunit2

cmd/utils.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,11 @@ func maybeFile(str string) bool {
227227
}
228228

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

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

0 commit comments

Comments
 (0)