Skip to content

Commit

Permalink
fix: guard resp against nil from http do (#237)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Nov 11, 2024
1 parent d1f1ae8 commit 68361c3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func getNodeMetrics(ctx context.Context) ([]byte, int, error) {
cfg.Prometheus.Host,
cfg.Prometheus.Port,
)
var respBodyBytes []byte
respBodyBytes := []byte{}
// Setup request
req, err := http.NewRequest(
http.MethodGet,
Expand All @@ -56,6 +56,11 @@ func getNodeMetrics(ctx context.Context) ([]byte, int, error) {
if err != nil {
return respBodyBytes, http.StatusInternalServerError, err
}
if resp == nil {
return respBodyBytes, http.StatusInternalServerError, fmt.Errorf(
"empty response",
)
}
// Read the entire response body and close it to prevent a memory leak
respBodyBytes, err = io.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 68361c3

Please sign in to comment.