Skip to content
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

fix segfault on gcp metadata server check #914

Merged
merged 1 commit into from
Jul 21, 2024
Merged
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
6 changes: 5 additions & 1 deletion internal/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ var style aurora.Aurora

func IsRunningInGCP() bool {
resp, err := http.Get("http://metadata.google.internal")
resp.Body.Close()

if resp != nil && resp.Body != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok but I don't think we need to check if the body is nil, only the response.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the body is nil the code defer resp.Body.Close() will panic

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but according to the documentation it won't be nil.

defer resp.Body.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be deferred, the body is not going to be used anymore.

}

return err == nil
}

Expand Down