diff --git a/internal/library/rtk_upgrade.go b/internal/library/rtk_upgrade.go index 7871d405f..5f0b3f879 100644 --- a/internal/library/rtk_upgrade.go +++ b/internal/library/rtk_upgrade.go @@ -302,7 +302,10 @@ func latestRTK() (string, error) { var r struct { Tag string `json:"tag_name"` } - body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + if err != nil { + return "", fmt.Errorf("couldn't read GitHub's answer: %w", err) + } if err := json.Unmarshal(body, &r); err != nil { return "", err } diff --git a/internal/library/rtk_upgrade_test.go b/internal/library/rtk_upgrade_test.go index f2f14b9a3..aed8b8627 100644 --- a/internal/library/rtk_upgrade_test.go +++ b/internal/library/rtk_upgrade_test.go @@ -57,6 +57,27 @@ func asJSON(t *testing.T, v *RTKView) map[string]any { return m } +// An answer cut off on the way is said so, not shown as a version that +// couldn't be read (#3045). +func TestLatestRTKCutAnswer(t *testing.T) { + sandbox(t) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Length", "1000") + w.Write([]byte(`{"tag_name": "v0.`)) + w.(http.Flusher).Flush() // the answer's head arrives, its body doesn't + panic(http.ErrAbortHandler) + })) + defer srv.Close() + old, oldAPI := rtkReleases, rtkReleasesAPI + rtkReleases, rtkReleasesAPI = srv.URL+"/latest", srv.URL + t.Cleanup(func() { rtkReleases, rtkReleasesAPI = old, oldAPI }) + if _, err := latestRTK(); err == nil { + t.Error("a cut-short answer wasn't noticed") + } else if !strings.Contains(err.Error(), "couldn't read GitHub's answer") { + t.Errorf("said: %v", err) + } +} + // Homebrew's rtk is upgraded with brew; the days it saved come with the // summary, oldest first; the latest release is read from where GitHub // redirects to. diff --git a/internal/library/skillcheck.go b/internal/library/skillcheck.go index 5c71dadd3..8e745d47d 100644 --- a/internal/library/skillcheck.go +++ b/internal/library/skillcheck.go @@ -203,6 +203,10 @@ func apiGet(u string, max int64) ([]byte, int, bool, error) { if resp.StatusCode == 200 && int64(len(body)) > max { return nil, resp.StatusCode, token, fmt.Errorf("GitHub's answer was over %s: %w", mb(max), errOverCap) } + // an empty 200 kept for a 304 would answer nothing forever (#3045) + if resp.StatusCode == 200 && len(body) == 0 { + return nil, resp.StatusCode, token, fmt.Errorf("GitHub answered nothing") + } if et := resp.Header.Get("ETag"); resp.StatusCode == 200 && et != "" && int64(len(body)) <= cacheBody { etags.Lock() etags.m[u] = etagged{etag: et, body: body} diff --git a/internal/library/skillcheck_test.go b/internal/library/skillcheck_test.go index f04df61fc..3abe14a7d 100644 --- a/internal/library/skillcheck_test.go +++ b/internal/library/skillcheck_test.go @@ -250,3 +250,16 @@ func TestApiGetWholeAnswer(t *testing.T) { t.Fatalf("502 over the cap: code %d, err %v", code, err) } } + +// An empty 200 is said so, not kept for a 304 to answer nothing with +// again (#3045). +func TestAPIGetEmptyAnswer(t *testing.T) { + sandbox(t) + empty := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer empty.Close() + if _, _, _, err := apiGet(empty.URL, 8<<20); err == nil { + t.Error("an empty answer wasn't noticed") + } else if !strings.Contains(err.Error(), "GitHub answered nothing") { + t.Errorf("said: %v", err) + } +}