From 76db162ab856366aad35011970743ebb6affcb81 Mon Sep 17 00:00:00 2001 From: Andre Detsch Date: Sat, 1 Nov 2025 14:11:49 -0300 Subject: [PATCH] transport: Avoid exception when printing warning after http error httpDoOnce may return nil for the response in case of errors. This was leading to a nil pointer dereference when trying to log the response. This patch adds a check to avoid accessing the response status code in that case. A second check is also added on the break condition, when err != nil. This should not be needed with the current httpDoOnce implementation, but is added for safety in case of future changes. Signed-off-by: Andre Detsch --- transport/http.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/transport/http.go b/transport/http.go index fc0d5ff..22830e1 100644 --- a/transport/http.go +++ b/transport/http.go @@ -96,11 +96,15 @@ func HttpDo(client *http.Client, method, url string, headers map[string]string, var res *HttpRes for _, delay := range []int{0, 1, 2, 5, 13, 30} { if delay != 0 { - slog.Warn("HTTP request failed, retrying", "url", url, "method", method, "delay", delay, "status", res.StatusCode, "error", err) + var status int + if res != nil { + status = res.StatusCode + } + slog.Warn("HTTP request failed, retrying", "url", url, "method", method, "delay", delay, "status", status, "error", err) time.Sleep(time.Second * time.Duration(delay)) } res, err = httpDoOnce(client, method, url, headers, data) - if err == nil && res.StatusCode != 0 && res.StatusCode < 500 { + if err == nil && res != nil && res.StatusCode != 0 && res.StatusCode < 500 { break } }