Commit 7579f966 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
Reviewed-by: default avatarRhys Hiltner <rhys@justin.tv>
Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 0aede739
...@@ -333,9 +333,11 @@ func (r *Request) WithContext(ctx context.Context) *Request { ...@@ -333,9 +333,11 @@ func (r *Request) WithContext(ctx context.Context) *Request {
// Deep copy the URL because it isn't // Deep copy the URL because it isn't
// a map and the URL is mutable by users // a map and the URL is mutable by users
// of WithContext. // of WithContext.
if r.URL != nil {
r2URL := new(url.URL) r2URL := new(url.URL)
*r2URL = *r.URL *r2URL = *r.URL
r2.URL = r2URL r2.URL = r2URL
}
return r2 return r2
} }
......
...@@ -799,6 +799,13 @@ func TestWithContextDeepCopiesURL(t *testing.T) { ...@@ -799,6 +799,13 @@ func TestWithContextDeepCopiesURL(t *testing.T) {
if firstURL == secondURL { if firstURL == secondURL {
t.Errorf("unexpected change to original request's URL") 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 // verify that NewRequest sets Request.GetBody and that it works
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment