Skip to content

fix: prevent panic when *http.Response is nil #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion remote_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func defaultRemoteFetchStrategy(remoteFetchHost string, versionStrategy VersionS

shaDownloadURL := fmt.Sprintf("%s.sha256", jarDownloadURL)
shaDownloadResponse, err := http.Get(shaDownloadURL)

if err != nil {
return fmt.Errorf("download sha256 from %s failed: %w", shaDownloadURL, err)
}
defer closeBody(shaDownloadResponse)()

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

func closeBody(resp *http.Response) func() {
return func() {
if resp == nil || resp.Body == nil {
return
}
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 6 additions & 0 deletions remote_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,9 @@ func Test_defaultRemoteFetchStrategy_whenContentLengthNotSet(t *testing.T) {
assert.NoError(t, err)
assert.FileExists(t, cacheLocation)
}

func Test_closeBody_NilResponse(t *testing.T) {
assert.NotPanics(t, func() {
closeBody(nil)()
})
}