From f2da8db11dac8fbf7448991299bbcb819da16ecc 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 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/releaseapi/client.go b/internal/releaseapi/client.go index 947dc23..e82d7e2 100644 --- a/internal/releaseapi/client.go +++ b/internal/releaseapi/client.go @@ -162,12 +162,11 @@ 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 {