From 631f7e7469929edd21e7ff738a2dd86564533a7f Mon Sep 17 00:00:00 2001 From: Sediman Date: Fri, 29 May 2026 05:06:17 +0200 Subject: [PATCH] fix: close response body in GitHub/GitLab API calls and fix typo Add defer res.Body.Close() after the first http.Get in both findGitHubREADME and findGitLabREADME. The API response body was being read with io.ReadAll but never closed, causing a resource leak. The existing nolint comments incorrectly stated the body was closed by the caller, but the caller only receives the second http.Get response body. Also fix a typo in the error message: 'Could not load find' -> 'Could not find'. --- github.go | 3 +-- gitlab.go | 3 +-- main.go | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/github.go b/github.go index fe862e3..2080ad4 100644 --- a/github.go +++ b/github.go @@ -23,12 +23,11 @@ func findGitHubREADME(u *url.URL) (*source, error) { apiURL := fmt.Sprintf("https://api.%s/repos/%s/%s/readme", u.Hostname(), owner, repo) - //nolint:bodyclose - // it is closed on the caller res, err := http.Get(apiURL) //nolint: gosec,noctx if err != nil { return nil, fmt.Errorf("unable to get url: %w", err) } + defer res.Body.Close() //nolint:bodyclose body, err := io.ReadAll(res.Body) if err != nil { diff --git a/gitlab.go b/gitlab.go index 68256be..e178050 100644 --- a/gitlab.go +++ b/gitlab.go @@ -25,12 +25,11 @@ func findGitLabREADME(u *url.URL) (*source, error) { apiURL := fmt.Sprintf("https://%s/api/v4/projects/%s", u.Hostname(), projectPath) - //nolint:bodyclose - // it is closed on the caller res, err := http.Get(apiURL) //nolint: gosec,noctx if err != nil { return nil, fmt.Errorf("unable to get url: %w", err) } + defer res.Body.Close() //nolint:bodyclose body, err := io.ReadAll(res.Body) if err != nil { diff --git a/main.go b/main.go index b31ca15..882436b 100644 --- a/main.go +++ b/main.go @@ -432,7 +432,7 @@ func tryLoadConfigFromDefaultPlaces() { scope := gap.NewScope(gap.User, "glow") dirs, err := scope.ConfigDirs() if err != nil { - fmt.Println("Could not load find configuration directory.") + fmt.Println("Could not find configuration directory.") os.Exit(1) }