Skip to content

Commit 4fb7ddc

Browse files
authored
fix: prevent panic when *http.Response is nil (#156)
* fix: prevent panic when *http.Response is nil Signed-off-by: Danny Kopping <[email protected]> * fix: add error handling for sha256 download Signed-off-by: Danny Kopping <[email protected]> * fix: check body nilness as well Signed-off-by: Danny Kopping <[email protected]> --------- Signed-off-by: Danny Kopping <[email protected]>
1 parent 6192f2a commit 4fb7ddc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

remote_fetch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func defaultRemoteFetchStrategy(remoteFetchHost string, versionStrategy VersionS
5050

5151
shaDownloadURL := fmt.Sprintf("%s.sha256", jarDownloadURL)
5252
shaDownloadResponse, err := http.Get(shaDownloadURL)
53-
53+
if err != nil {
54+
return fmt.Errorf("download sha256 from %s failed: %w", shaDownloadURL, err)
55+
}
5456
defer closeBody(shaDownloadResponse)()
5557

5658
if err == nil && shaDownloadResponse.StatusCode == http.StatusOK {
@@ -68,6 +70,9 @@ func defaultRemoteFetchStrategy(remoteFetchHost string, versionStrategy VersionS
6870

6971
func closeBody(resp *http.Response) func() {
7072
return func() {
73+
if resp == nil || resp.Body == nil {
74+
return
75+
}
7176
if err := resp.Body.Close(); err != nil {
7277
log.Fatal(err)
7378
}

remote_fetch_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,9 @@ func Test_defaultRemoteFetchStrategy_whenContentLengthNotSet(t *testing.T) {
417417
assert.NoError(t, err)
418418
assert.FileExists(t, cacheLocation)
419419
}
420+
421+
func Test_closeBody_NilResponse(t *testing.T) {
422+
assert.NotPanics(t, func() {
423+
closeBody(nil)()
424+
})
425+
}

0 commit comments

Comments
 (0)