Merge 3aaa54b96a9cb20a44e41ac652db0f89a0959a79 into e8f24969d59611428b03d02424b8f4bb55a1ef91

This commit is contained in:
adam magued 2026-10-10 12:17:43 +08:00 committed by GitHub
commit 456b522353
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 8 deletions

View File

@ -847,6 +847,30 @@ func TestEngineHandleMethodNotAllowedCornerCase(t *testing.T) {
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestLateRouteRegistrationSkippedNodes(t *testing.T) {
router := New()
router.HandleMethodNotAllowed = true
router.GET("/a", func(c *Context) { c.Status(http.StatusOK) })
w := PerformRequest(router, http.MethodGet, "/a")
if w.Code != http.StatusOK {
t.Fatalf("warmup status = %d, want 200", w.Code)
}
h := func(c *Context) {}
router.GET("/x/y/z/w", h)
router.GET("/x/y/:id/w", h)
router.GET("/x/:id/z/w", h)
router.GET("/:id/y/z/w", h)
req := httptest.NewRequest(http.MethodPost, "/x/y/z/w/extra", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("status = %d, want 404", w.Code)
}
}
// TestIssue4818_skippedNodesOverflow_Panic reproduces the panic from issue #4818:
// with HandleMethodNotAllowed enabled, getValue is called once per method tree
// reusing the same c.skippedNodes stack without resetting it, so residue

14
tree.go
View File

@ -420,10 +420,10 @@ func (n *node) getValue(path string, params *Params, skippedNodes *[]skippedNode
// Reset the skipped-nodes stack on entry. getValue is called once per
// method tree (e.g. in the HandleMethodNotAllowed loop) reusing the same
// pooled Context stack, and the walk below grows it via a raw reslice that
// cannot exceed engine.maxSections. Without this reset the residue from a
// previous tree leaks into the next call and can panic with "slice bounds
// out of range" once the accumulated length passes the capacity.
// pooled Context stack. Without this reset the residue from a previous
// tree leaks into the next call. The walk grows the stack with append so
// that pooled contexts dynamically expand when routes registered late
// require more backtrack frames.
*skippedNodes = (*skippedNodes)[:0]
walk: // Outer loop for walking the tree
@ -439,9 +439,7 @@ walk: // Outer loop for walking the tree
if c == idxc {
// strings.HasPrefix(n.children[len(n.children)-1].path, ":") == n.wildChild
if n.wildChild {
index := len(*skippedNodes)
*skippedNodes = (*skippedNodes)[:index+1]
(*skippedNodes)[index] = skippedNode{
*skippedNodes = append(*skippedNodes, skippedNode{
path: prefix + path,
node: &node{
path: n.path,
@ -453,7 +451,7 @@ walk: // Outer loop for walking the tree
fullPath: n.fullPath,
},
paramsCount: globalParamsCount,
}
})
}
n = n.children[i]

View File

@ -1111,3 +1111,25 @@ func TestTreeFindCaseInsensitivePathWildcardParamAndStaticChild(t *testing.T) {
t.Errorf("Wrong result for '/prefix/something': %s", string(out))
}
}
func TestTreeSkippedNodesDynamicGrowth(t *testing.T) {
tree := &node{}
routes := [...]string{
"/x/y/z/w",
"/x/y/:id/w",
"/x/:id/z/w",
"/:id/y/z/w",
}
for _, route := range routes {
tree.addRoute(route, fakeHandler(route))
}
skippedNodes := make([]skippedNode, 0, 1)
val := tree.getValue("/x/y/z/w/extra", nil, &skippedNodes, false)
if val.handlers != nil {
t.Fatalf("expected nil handlers, got %v", val.handlers)
}
if cap(skippedNodes) <= 1 {
t.Fatalf("expected skippedNodes capacity to grow beyond 1, got %d", cap(skippedNodes))
}
}