From f4dfe65b58fb759c618b3d94890c8bca3af127da Mon Sep 17 00:00:00 2001 From: Kristian Svalland Date: Tue, 3 Jan 2023 10:37:25 +0100 Subject: [PATCH] tree: Fix bug where loop index is not decremented. Also add test to validate fix. fixes https://github.com/gin-gonic/gin/issues/3459 Signed-off-by: Kristian Svalland --- routes_test.go | 19 +++++++++++++++++++ tree.go | 3 +++ 2 files changed, 22 insertions(+) diff --git a/routes_test.go b/routes_test.go index cd8cf14145..ada8e1e457 100644 --- a/routes_test.go +++ b/routes_test.go @@ -670,3 +670,22 @@ func TestRouteContextHoldsFullPath(t *testing.T) { w := PerformRequest(router, http.MethodGet, "/not-found") assert.Equal(t, http.StatusNotFound, w.Code) } + +func TestEngineHandleMethodNotAllowedCornerCase(t *testing.T) { + r := New() + r.HandleMethodNotAllowed = true + + base := r.Group("base") + base.GET("/metrics", handlerTest1) + + v1 := base.Group("v1") + + v1.GET("/:id/devices", handlerTest1) + v1.GET("/user/:id/groups", handlerTest1) + + v1.GET("/orgs/:id", handlerTest1) + v1.DELETE("/orgs/:id", handlerTest1) + + w := PerformRequest(r, "GET", "/base/v1/user/groups") + assert.Equal(t, http.StatusNotFound, w.Code) +} diff --git a/tree.go b/tree.go index 3f34b8ee82..ff5c3f4405 100644 --- a/tree.go +++ b/tree.go @@ -462,6 +462,7 @@ walk: // Outer loop for walking the tree for l := len(*skippedNodes); l > 0; { skippedNode := (*skippedNodes)[l-1] *skippedNodes = (*skippedNodes)[:l-1] + l-- if strings.HasSuffix(skippedNode.path, path) { path = skippedNode.path n = skippedNode.node @@ -579,6 +580,7 @@ walk: // Outer loop for walking the tree for l := len(*skippedNodes); l > 0; { skippedNode := (*skippedNodes)[l-1] *skippedNodes = (*skippedNodes)[:l-1] + l-- if strings.HasSuffix(skippedNode.path, path) { path = skippedNode.path n = skippedNode.node @@ -636,6 +638,7 @@ walk: // Outer loop for walking the tree for l := len(*skippedNodes); l > 0; { skippedNode := (*skippedNodes)[l-1] *skippedNodes = (*skippedNodes)[:l-1] + l-- if strings.HasSuffix(skippedNode.path, path) { path = skippedNode.path n = skippedNode.node