Repository: gin-gonic/gin
Pull request #2755 (2021-06-19 12:16:48)

Fix insufficient slice check
- With pull requests:
  - Open your pull request against `master`
  - Your pull request should have no more than two commits, if not you should squash them.
  - It should pass all tests in the available continuous integration systems such as TravisCI.
  - You should add/modify tests to cover your proposed code changes.
  - If your pull request contains a new feature, please document it on the README.



mamil: I saw this bug mentioned in issue [2690](https://github.com/gin-gonic/gin/issues/2690), so I created this PR to fix the issue
thinkerou: > I saw this bug mentioned in issue [2690](https://github.com/gin-gonic/gin/issues/2690), so I created this PR to fix the issue

@mamil add one test case using #2690 ? thanks a lot!
mamil: > @mamil add one test case using #2690 ? thanks a lot!

@thinkerou case added, please have a look
thinkerou: @mamil unit test success? run error on my mac
appleboy: @mamil build fail.
mamil: @appleboy  @thinkerou UT fixed, sorry for the confusing
codecov[bot]: # [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) Report
> Merging [#2755](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) (11f3091) into [master](https://codecov.io/gh/gin-gonic/gin/commit/34ce2104cad324f444943c528746bf6d23643cd3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) (34ce210) will **decrease** coverage by `0.14%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/gin-gonic/gin/pull/2755/graphs/tree.svg?width=650&height=150&src=pr&token=Qpqxhwery8&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)

```diff
@@            Coverage Diff             @@
##           master    #2755      +/-   ##
==========================================
- Coverage   98.69%   98.55%   -0.15%     
==========================================
  Files          41       41              
  Lines        2072     2077       +5     
==========================================
+ Hits         2045     2047       +2     
- Misses         15       17       +2     
- Partials       12       13       +1     
```


| [Impacted Files](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) | Coverage Δ | |
|---|---|---|
| [tree.go](https://codecov.io/gh/gin-gonic/gin/pull/2755/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-dHJlZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [debug.go](https://codecov.io/gh/gin-gonic/gin/pull/2755/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-ZGVidWcuZ28=) | `92.30% <0.00%> (-7.70%)` | :arrow_down: |
| [recovery.go](https://codecov.io/gh/gin-gonic/gin/pull/2755/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-cmVjb3ZlcnkuZ28=) | `97.14% <0.00%> (-0.05%)` | :arrow_down: |
| [gin.go](https://codecov.io/gh/gin-gonic/gin/pull/2755/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-Z2luLmdv) | `99.18% <0.00%> (ø)` | |
| [context.go](https://codecov.io/gh/gin-gonic/gin/pull/2755/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-Y29udGV4dC5nbw==) | `97.69% <0.00%> (+0.03%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic). Last update [34ce210...11f3091](https://codecov.io/gh/gin-gonic/gin/pull/2755?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic).


Patch:
diff --git a/render/msgpack.go b/render/msgpack.go
index 6ef5b6e..7f17ca4 100644
--- a/render/msgpack.go
+++ b/render/msgpack.go
@@ -13,6 +13,8 @@ import (
 	"github.com/ugorji/go/codec"
 )
 
+// Check interface implemented here to support go build tag nomsgpack.
+// See: https://github.com/gin-gonic/gin/pull/1852/
 var (
 	_ Render = MsgPack{}
 )
diff --git a/tree.go b/tree.go
index 5f35fff..4b5656f 100644
--- a/tree.go
+++ b/tree.go
@@ -459,7 +459,7 @@ walk: // Outer loop for walking the tree
 					}
 
 					// Save param value
-					if params != nil {
+					if params != nil && cap(*params) > 0 {
 						if value.params == nil {
 							value.params = params
 						}


Test patch:
diff --git a/render/render_msgpack_test.go b/render/render_msgpack_test.go
index 8170fbe..d16cf6e 100644
--- a/render/render_msgpack_test.go
+++ b/render/render_msgpack_test.go
@@ -39,6 +39,6 @@ func TestRenderMsgPack(t *testing.T) {
 	err = codec.NewEncoder(buf, h).Encode(data)
 
 	assert.NoError(t, err)
-	assert.Equal(t, w.Body.String(), string(buf.Bytes()))
+	assert.Equal(t, w.Body.String(), buf.String())
 	assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
 }
diff --git a/tree_test.go b/tree_test.go
index 298c5ed..7459317 100644
--- a/tree_test.go
+++ b/tree_test.go
@@ -717,6 +717,19 @@ func TestTreeInvalidNodeType(t *testing.T) {
 	}
 }
 
+func TestTreeInvalidParamsType(t *testing.T) {
+	tree := &node{}
+	tree.wildChild = true
+	tree.children = append(tree.children, &node{})
+	tree.children[0].nType = 2
+
+	// set invalid Params type
+	params := make(Params, 0, 0)
+
+	// try to trigger slice bounds out of range with capacity 0
+	tree.getValue("/test", &params, false)
+}
+
 func TestTreeWildcardConflictEx(t *testing.T) {
 	conflicts := [...]struct {
 		route        string