Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/net/http/httputil/reverseproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TestReverseProxy(t *testing.T) {
if g, e := res.Trailer.Get("X-Unannounced-Trailer"), "unannounced_trailer_value"; g != e {
t.Errorf("Trailer(X-Unannounced-Trailer) = %q ; want %q", g, e)
}
res.Body.Close()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bito Code Review Agent Run #17d05d - 07/19/2024, 11:54 am

🔴 High importance
Issue: The 'res.Body.Close()' call should be deferred immediately after the 'res, err := frontendClient.Do(getReq)' call to ensure that the response body is closed even if an error occurs later in the function.
Fix: Defer the 'res.Body.Close()' call immediately after the 'res, err := frontendClient.Do(getReq)' call to ensure the response body is closed properly.
Code suggestion
 @@ -102,6 +102,7 @@
   res, err := frontendClient.Do(getReq)
   if err != nil {
       t.Fatalf("Get: %v", err)
   }
 + defer res.Body.Close()
   if g, e := res.StatusCode, backendStatus; g != e {
       t.Errorf("got res.StatusCode %d; expected %d", g, e)
   }
 @@ -139,7 +140,6 @@
   }
   if g, e := res.Trailer.Get("X-Unannounced-Trailer"), "unannounced_trailer_value"; g != e {
       t.Errorf("Trailer(X-Unannounced-Trailer) = %q ; want %q", g, e)
   }
 - res.Body.Close()

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged


// Test that a backend failing to be reached or one which doesn't return
// a response results in a StatusBadGateway.
Expand Down Expand Up @@ -328,6 +329,7 @@ func TestXForwardedFor(t *testing.T) {
if err != nil {
t.Fatalf("Get: %v", err)
}
defer res.Body.Close()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bito Code Review Agent Run #17d05d - 07/19/2024, 11:54 am

🔴 High importance
Issue: The defer statement to close the response body is placed after a potential early return due to an error check. This means that if an error occurs, the response body will not be closed, leading to a resource leak. A similar issue was also found in src/net/http/httputil/reverseproxy_test.go (line 806).
Fix: Move the defer statement to close the response body immediately after the response is received, before any error checks.
Code suggestion
 @@ -328,7 +328,7 @@
  res, err := frontend.Client().Do(getReq)
  if err != nil {
      t.Fatalf("Get: %v", err)
  }
 +defer res.Body.Close()
  if g, e := res.StatusCode, backendStatus; g != e {
      t.Errorf("got res.StatusCode %d; expected %d", g, e)
  }

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

if g, e := res.StatusCode, backendStatus; g != e {
t.Errorf("got res.StatusCode %d; expected %d", g, e)
}
Expand Down Expand Up @@ -801,6 +803,7 @@ func TestReverseProxy_Post(t *testing.T) {
if err != nil {
t.Fatalf("Do: %v", err)
}
defer res.Body.Close()
if g, e := res.StatusCode, backendStatus; g != e {
t.Errorf("got res.StatusCode %d; expected %d", g, e)
}
Expand Down Expand Up @@ -1571,7 +1574,7 @@ func TestUnannouncedTrailer(t *testing.T) {
}

io.ReadAll(res.Body)

res.Body.Close()
if g, w := res.Trailer.Get("X-Unannounced-Trailer"), "unannounced_trailer_value"; g != w {
t.Errorf("Trailer(X-Unannounced-Trailer) = %q; want %q", g, w)
}
Expand Down