aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordqu123 <davidqu12345@gmail.com>2020-10-10 16:25:07 -0400
committerCarlos Amedee <carlos@golang.org>2020-10-23 21:35:30 +0000
commit1a05c910fbf62f6f06930394853e9cf7ca6087c6 (patch)
treeff70c867e993a3272ab92738cd0bd36407d9f20f
parentf68af196a36e740a56121dc759d1f0f3ab7749c6 (diff)
downloadgo-1a05c910fbf62f6f06930394853e9cf7ca6087c6.tar.gz
go-1a05c910fbf62f6f06930394853e9cf7ca6087c6.zip
[release-branch.go1.15] net/http: deep copy Request.TransferEncoding
The existing implementation in Request.Clone() assigns the wrong pointer to r2.TransferEncoding. Updates #41907. Fixes #41914. Change-Id: I7f220a41b1b46a55d1a1005e47c6dd69478cb025 Reviewed-on: https://go-review.googlesource.com/c/go/+/261378 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Carlos Amedee <carlos@golang.org> Trust: Emmanuel Odeke <emm.odeke@gmail.com>
-rw-r--r--src/net/http/request.go2
-rw-r--r--src/net/http/request_test.go21
2 files changed, 22 insertions, 1 deletions
diff --git a/src/net/http/request.go b/src/net/http/request.go
index fe6b60982c..54ec1c5593 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -382,7 +382,7 @@ func (r *Request) Clone(ctx context.Context) *Request {
if s := r.TransferEncoding; s != nil {
s2 := make([]string, len(s))
copy(s2, s)
- r2.TransferEncoding = s
+ r2.TransferEncoding = s2
}
r2.Form = cloneURLValues(r.Form)
r2.PostForm = cloneURLValues(r.PostForm)
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index 42c16d00ea..461d66e05d 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -828,6 +828,27 @@ func TestWithContextDeepCopiesURL(t *testing.T) {
}
}
+// Ensure that Request.Clone creates a deep copy of TransferEncoding.
+// See issue 41907.
+func TestRequestCloneTransferEncoding(t *testing.T) {
+ body := strings.NewReader("body")
+ req, _ := NewRequest("POST", "https://example.org/", body)
+ req.TransferEncoding = []string{
+ "encoding1",
+ }
+
+ clonedReq := req.Clone(context.Background())
+ // modify original after deep copy
+ req.TransferEncoding[0] = "encoding2"
+
+ if req.TransferEncoding[0] != "encoding2" {
+ t.Error("expected req.TransferEncoding to be changed")
+ }
+ if clonedReq.TransferEncoding[0] != "encoding1" {
+ t.Error("expected clonedReq.TransferEncoding to be unchanged")
+ }
+}
+
func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
testNoPanicWithBasicAuth(t, h1Mode)
}