From eba734e22726fef21bcd9b51e466879a100230cb Mon Sep 17 00:00:00 2001 From: Nicholas DiBari Date: Thu, 11 Jan 2024 14:42:40 -0500 Subject: [PATCH] fix: check err before using file object If there is an error downloading a release archive from Hashicorp, the `zipFile` variable will be `nil` and cause a panic on the deferred call to `os.Remove` due to a nil pointer dereference. This hides the underlying error and prevents the user from knowing what the actual problem is in downloading the release archive. This change moves the order of operations around to check if there is an error returned by `downloadReleaseArchive` first before using the `zipFile` variable. In the case that there was a problem downloading the release archive from Hashicorp this function should return the originating error and not mask it with a nil pointer dereference. --- internal/releaseapi/client.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/releaseapi/client.go b/internal/releaseapi/client.go index 947dc23..62d8642 100644 --- a/internal/releaseapi/client.go +++ b/internal/releaseapi/client.go @@ -162,17 +162,20 @@ func (c *Client) downloadBuild(build Build, checkSha256Sum string) (string, erro log.Printf("dowloading release archive from %s", build.URL) zipFile, zipLength, err := c.downloadReleaseArchive(build) - defer os.Remove(zipFile.Name()) - defer zipFile.Close() if err != nil { return "", err } + defer os.Remove(zipFile.Name()) + defer zipFile.Close() + f, err := os.Open(zipFile.Name()) + if err != nil { return "", errors.Wrap(err, "could not open zip archive") } + defer f.Close() h := sha256.New()