-
Notifications
You must be signed in to change notification settings - Fork 173
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
Conversation
internal/gcs/gcs.go
Outdated
if err != nil { | ||
return false | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
return true |
There was a problem hiding this comment.
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
if err != nil { | |
return false | |
} | |
defer resp.Body.Close() | |
return true | |
if resp != nil && resp.Body != nil { | |
defer resp.Body.Close() | |
} | |
return err == nil |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
should fix #909