From: Brad Fitzpatrick Date: Wed, 7 Jun 2017 18:13:38 +0000 (+0000) Subject: net/http: don't crash in Request.WithContext if Request.URL is nil X-Git-Tag: go1.9beta1~90 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7579f96676eb2ea932cfa02e805bbae02c0d310c;p=gostls13.git net/http: don't crash in Request.WithContext if Request.URL is nil Fixes #20601 Change-Id: I296d50dc5210a735a2a65d64bfef05d14c93057b Reviewed-on: https://go-review.googlesource.com/45073 Run-TryBot: Brad Fitzpatrick Reviewed-by: Rhys Hiltner Reviewed-by: Emmanuel Odeke Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/net/http/request.go b/src/net/http/request.go index 7f473dd15d..c493aeb2d7 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -333,9 +333,11 @@ func (r *Request) WithContext(ctx context.Context) *Request { // Deep copy the URL because it isn't // a map and the URL is mutable by users // of WithContext. - r2URL := new(url.URL) - *r2URL = *r.URL - r2.URL = r2URL + if r.URL != nil { + r2URL := new(url.URL) + *r2URL = *r.URL + r2.URL = r2URL + } return r2 } diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go index 1608d1c4fe..967156bac9 100644 --- a/src/net/http/request_test.go +++ b/src/net/http/request_test.go @@ -799,6 +799,13 @@ func TestWithContextDeepCopiesURL(t *testing.T) { if firstURL == secondURL { t.Errorf("unexpected change to original request's URL") } + + // And also check we don't crash on nil (Issue 20601) + req.URL = nil + reqCopy = req.WithContext(context.Background()) + if reqCopy.URL != nil { + t.Error("expected nil URL in cloned request") + } } // verify that NewRequest sets Request.GetBody and that it works