Repository: cloudwego/hertz
Pull request #542 (2023-01-13 10:37:10)

fix: `CopyTo` doesn't copy bodyraw deeply
#### What type of PR is this?
<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->
fix
#### Check the PR title.
<!--
The description of the title will be attached in Release Notes, 
so please describe it from user-oriented, what this PR does / why we need it.
Please check your PR title with the below requirements:
-->
- [ ] This PR title match the format: \<type\>(optional scope): \<description\>
- [ ] The description of this PR title is user-oriented and clear enough for others to understand.


#### (Optional) Translate the PR title into Chinese.
`CopyTo`  没有深拷贝 `bodyRaw`

#### (Optional) More detail description for this PR(en: English/zh: Chinese).
<!--
Provide more detailed info for review. If it is a perf type PR, perf data is suggested to give.
-->
en: `CopyTo` doesn't copy `bodyraw` deeply
zh(optional): `CopyTo`  没有深拷贝 `bodyRaw`

#### Which issue(s) this PR fixes:
<!--
Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->


codecov[bot]: # [Codecov](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
Base: **69.87**% // Head: **69.89**% // Increases project coverage by **`+0.02%`** :tada:
> Coverage data is based on head [(`7a23ff7`)](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) compared to base [(`6e0a35d`)](https://codecov.io/gh/cloudwego/hertz/commit/6e0a35d48c6832cad35148aa355668f3ff1f2c7c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop     #542      +/-   ##
===========================================
+ Coverage    69.87%   69.89%   +0.02%     
===========================================
  Files           93       93              
  Lines         8887     8887              
===========================================
+ Hits          6210     6212       +2     
+ Misses        2307     2306       -1     
+ Partials       370      369       -1     
```


| [Impacted Files](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) | Coverage Δ | |
|---|---|---|
| [pkg/protocol/request.go](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL3Byb3RvY29sL3JlcXVlc3QuZ28=) | `84.29% <100.00%> (+1.04%)` | :arrow_up: |
| [pkg/protocol/response.go](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL3Byb3RvY29sL3Jlc3BvbnNlLmdv) | `84.10% <100.00%> (+1.32%)` | :arrow_up: |
| [pkg/common/timer/timer.go](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL2NvbW1vbi90aW1lci90aW1lci5nbw==) | `61.90% <0.00%> (-19.05%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/cloudwego/hertz/pull/542?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).


Patch:
diff --git a/pkg/protocol/request.go b/pkg/protocol/request.go
index 52f28d9..8b86067 100644
--- a/pkg/protocol/request.go
+++ b/pkg/protocol/request.go
@@ -347,7 +347,7 @@ func (req *Request) SwapBody(body []byte) []byte {
 func (req *Request) CopyTo(dst *Request) {
 	req.CopyToSkipBody(dst)
 	if req.bodyRaw != nil {
-		dst.bodyRaw = req.bodyRaw
+		dst.bodyRaw = append(dst.bodyRaw[:0], req.bodyRaw...)
 		if dst.body != nil {
 			dst.body.Reset()
 		}
diff --git a/pkg/protocol/response.go b/pkg/protocol/response.go
index f46395b..04440f3 100644
--- a/pkg/protocol/response.go
+++ b/pkg/protocol/response.go
@@ -240,7 +240,7 @@ func (resp *Response) BodyWriteTo(w io.Writer) error {
 func (resp *Response) CopyTo(dst *Response) {
 	resp.CopyToSkipBody(dst)
 	if resp.bodyRaw != nil {
-		dst.bodyRaw = resp.bodyRaw
+		dst.bodyRaw = append(dst.bodyRaw[:0], resp.bodyRaw...)
 		if dst.body != nil {
 			dst.body.Reset()
 		}


Test patch:
diff --git a/pkg/protocol/request_test.go b/pkg/protocol/request_test.go
index 71fa8fb..850b73d 100644
--- a/pkg/protocol/request_test.go
+++ b/pkg/protocol/request_test.go
@@ -748,3 +748,18 @@ func testBodyWriteTo(t *testing.T, bw bodyWriterTo, expectedS string, isRetained
 		}
 	}
 }
+
+func TestReqSafeCopy(t *testing.T) {
+	req := AcquireRequest()
+	req.bodyRaw = make([]byte, 1)
+	reqs := make([]*Request, 10)
+	for i := 0; i < 10; i++ {
+		req.bodyRaw[0] = byte(i)
+		tmpReq := AcquireRequest()
+		req.CopyTo(tmpReq)
+		reqs[i] = tmpReq
+	}
+	for i := 0; i < 10; i++ {
+		assert.DeepEqual(t, []byte{byte(i)}, reqs[i].Body())
+	}
+}
diff --git a/pkg/protocol/response_test.go b/pkg/protocol/response_test.go
index 7d88e0b..1ecb806 100644
--- a/pkg/protocol/response_test.go
+++ b/pkg/protocol/response_test.go
@@ -257,3 +257,18 @@ func TestSetBodyStreamNoReset(t *testing.T) {
 	resp.SetBodyStream(bsC, 1)
 	assert.DeepEqual(t, bsA.String(), "")
 }
+
+func TestRespSafeCopy(t *testing.T) {
+	resp := AcquireResponse()
+	resp.bodyRaw = make([]byte, 1)
+	resps := make([]*Response, 10)
+	for i := 0; i < 10; i++ {
+		resp.bodyRaw[0] = byte(i)
+		tmpResq := AcquireResponse()
+		resp.CopyTo(tmpResq)
+		resps[i] = tmpResq
+	}
+	for i := 0; i < 10; i++ {
+		assert.DeepEqual(t, []byte{byte(i)}, resps[i].Body())
+	}
+}