From 68361c37931a48c53a125fb8046daeed0b288847 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sun, 10 Nov 2024 22:05:27 -0500 Subject: [PATCH] fix: guard resp against nil from http do (#237) Signed-off-by: Chris Gianelloni --- env.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/env.go b/env.go index 381b773..79369b0 100644 --- a/env.go +++ b/env.go @@ -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, @@ -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 {