Skip to content

Commit

Permalink
fix: check err before using file object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ndibari-etsy committed Jan 11, 2024
1 parent 2177088 commit f2da8db
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions internal/releaseapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f2da8db

Please sign in to comment.