mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-06 10:04:43 +08:00
readStream opened an *os.File but returned a *bufio.Reader, so the *os.File type switch in streamEvaluator.EvaluateFiles never matched and no input file was ever closed. allAtOnceEvaluator.EvaluateFiles did not attempt to close them at all. Descriptors were only released at process exit or by the os.File finaliser, so a multi-file run could exhaust the file descriptor limit. readStream now returns an explicit cleanup function alongside the reader, which both evaluators call once the file has been processed. Co-authored-by: Pablo Garcia <pablito@MacBook-Neo-de-Pablo.local> Co-authored-by: Mike Farah <mikefarah@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Pablo Garcia
Mike Farah
Cursor
parent
2ba1b902aa
commit
cb7b707457
+12
-13
@@ -32,21 +32,20 @@ func resolveFilename(filename string) string {
|
||||
return filename
|
||||
}
|
||||
|
||||
func readStream(filename string) (io.Reader, error) {
|
||||
var reader *bufio.Reader
|
||||
// readStream returns a reader for the given file, along with a cleanup function
|
||||
// that must be called once the reader is no longer needed. The cleanup is a no-op
|
||||
// for stdin.
|
||||
func readStream(filename string) (io.Reader, func(), error) {
|
||||
if filename == "-" {
|
||||
reader = bufio.NewReader(os.Stdin)
|
||||
} else {
|
||||
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
|
||||
// and ensuring that it's not possible to give a path to a file outside that directory.
|
||||
file, err := os.Open(filename) // #nosec
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader = bufio.NewReader(file)
|
||||
return bufio.NewReader(os.Stdin), func() {}, nil
|
||||
}
|
||||
return reader, nil
|
||||
|
||||
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
|
||||
// and ensuring that it's not possible to give a path to a file outside that directory.
|
||||
file, err := os.Open(filename) // #nosec
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return bufio.NewReader(file), func() { safelyCloseFile(file) }, nil
|
||||
}
|
||||
|
||||
func writeString(writer io.Writer, txt string) error {
|
||||
|
||||
Reference in New Issue
Block a user