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

Conversation

ksawarzynski
Copy link
Contributor

should fix #909

Comment on lines 22 to 28
if err != nil {
return false
}

defer resp.Body.Close()

return true

Choose a reason for hiding this comment

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

I would suggest to avoid the return true, and return false pattern

Suggested change
if err != nil {
return false
}
defer resp.Body.Close()
return true
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return err == nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

resp.Body.Close()

if resp != nil && resp.Body != 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.

@@ -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.

@luisdavim luisdavim merged commit cce7e31 into Praqma:master Jul 21, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

segfault when using gcs repository
3 participants