From 5bbd10857250a2c9bf5559ebd9bdc8aa2f144bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=A3o=20Gomes=20Viana?= Date: Thu, 11 Nov 2021 14:32:10 +0100 Subject: [PATCH] funcs: defer close file in funcs Files opened by these two functions were not being closed, leaking file descriptors. Close files that were opened when the function exist. --- internal/lang/funcs/crypto.go | 1 + internal/lang/funcs/filesystem.go | 1 + 2 files changed, 2 insertions(+) diff --git a/internal/lang/funcs/crypto.go b/internal/lang/funcs/crypto.go index f785a7f3b9..7c4ba4ada3 100644 --- a/internal/lang/funcs/crypto.go +++ b/internal/lang/funcs/crypto.go @@ -248,6 +248,7 @@ func makeFileHashFunction(baseDir string, hf func() hash.Hash, enc func([]byte) if err != nil { return cty.UnknownVal(cty.String), err } + defer f.Close() h := hf() _, err = io.Copy(h, f) diff --git a/internal/lang/funcs/filesystem.go b/internal/lang/funcs/filesystem.go index fb0db7e53a..846b86110f 100644 --- a/internal/lang/funcs/filesystem.go +++ b/internal/lang/funcs/filesystem.go @@ -377,6 +377,7 @@ func readFileBytes(baseDir, path string) ([]byte, error) { } return nil, err } + defer f.Close() src, err := ioutil.ReadAll(f) if err != nil {