]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: don't crash in Request.WithContext if Request.URL is nil
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 7 Jun 2017 18:13:38 +0000 (18:13 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 7 Jun 2017 19:27:19 +0000 (19:27 +0000)
Fixes #20601

Change-Id: I296d50dc5210a735a2a65d64bfef05d14c93057b
Reviewed-on: https://go-review.googlesource.com/45073
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rhys Hiltner <rhys@justin.tv>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/request.go
src/net/http/request_test.go

index 7f473dd15ded6dcd72b5424e15ddaafeee6d24fd..c493aeb2d7bfb6849298d0e130cf58c7d45e960f 100644 (file)
@@ -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
 }
index 1608d1c4fed979b82b6855124fa4d41e053f07d5..967156bac9b6228167ac03824d3741eb25b8d68e 100644 (file)
@@ -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