Skip to content

Commit 12d02e7

Browse files
author
Bryan C. Mills
committed
net/http: verify RoundTripper invariants in the send function
Issue #37598 reports a nil-panic in *Client.send that can only occur if one of the RoundTripper invariants is violated. Unfortunately, that condition is currently difficult to diagnose: it manifests as a panic during a Response field access, rather than something the user can easily associate with an specific erroneous RoundTripper implementation. No test because the new code paths are supposed to be unreachable. Updates #37598 Change-Id: If0451e9c6431f6fab7137de43727297a80def05b Reviewed-on: https://go-review.googlesource.com/c/go/+/221818 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent acac535 commit 12d02e7

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/net/http/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ func send(ireq *Request, rt RoundTripper, deadline time.Time) (resp *Response, d
265265
}
266266
return nil, didTimeout, err
267267
}
268+
if resp == nil {
269+
return nil, didTimeout, fmt.Errorf("http: RoundTripper implementation (%T) returned a nil *Response with a nil error", rt)
270+
}
271+
if resp.Body == nil {
272+
return nil, didTimeout, fmt.Errorf("http: RoundTripper implementation (%T) returned a *Response with a nil Body", rt)
273+
}
268274
if !deadline.IsZero() {
269275
resp.Body = &cancelTimerBody{
270276
stop: stopTimer,

0 commit comments

Comments
 (0)