From 7e3f82cbd7b3872b293dbf90d66d3f46376fc4b3 Mon Sep 17 00:00:00 2001 From: paidax <2338239869@qq.com> Date: Sun, 6 Sep 2026 20:47:46 +0800 Subject: [PATCH 1/2] fix(gin): reset skippedNodes on getValue entry to prevent 405 loop panic skippedNodes is pooled per Context with a fixed capacity (maxSections) and only cleared once per request in Context.reset(). With HandleMethodNotAllowed enabled, handleHTTPRequest walks one method tree after another reusing the same slice, and getValue leaves entries pushed on a successful match (pops only run on failure paths). The residue accumulates across trees until the raw reslice (*skippedNodes)[:index+1] exceeds capacity: panic: runtime error: slice bounds out of range [:7] with capacity 6 The panic happens during route matching, before the handler chain, so gin.Recovery() cannot catch it and clients get a closed connection with no response. Start each getValue walk from an empty list: skipped nodes are only meaningful within a single walk and no caller reads the slice across calls. Fixes #4818 --- gin_test.go | 26 ++++++++++++++++++++++++++ tree.go | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/gin_test.go b/gin_test.go index a9cf1755..15a66748 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1047,6 +1047,32 @@ func TestCustomUnmarshalStruct(t *testing.T) { assert.Equal(t, `"2000/01/01"`, w.Body.String()) } +// Test the fix for https://github.com/gin-gonic/gin/issues/4818 +func TestMethodNotAllowedSkippedNodesOverflow(t *testing.T) { + SetMode(TestMode) + router := New() + router.HandleMethodNotAllowed = true + + h := func(c *Context) {} + router.OPTIONS("/:p0/:p1/a/:p2", h) + router.GET("/:p0/:p1/a/:p2", h) + router.PATCH("/b/:p0/:p1/c", h) + router.DELETE("/b/:p0/:p1/d/:p3", h) + router.GET("/b/:p0/:p1/e/f", h) + router.POST("/b/:p0/:p1/g/:p4/h", h) + router.OPTIONS("/b/:p0/:p1/g/:p4/h", h) + router.DELETE("/b/cache", h) + router.GET("/b/clients/:p1/g", h) + router.POST("/b/clients/:p1/g", h) + router.PATCH("/b/clients/:p1/g/:p4", h) + router.OPTIONS("/b/clients/:p1/g/:p4", h) + + req := httptest.NewRequest(http.MethodPost, "/b/clients/42", nil) + resp := httptest.NewRecorder() + assert.NotPanics(t, func() { router.ServeHTTP(resp, req) }) + assert.Equal(t, http.StatusNotFound, resp.Code) +} + // Test the fix for https://github.com/gin-gonic/gin/issues/4002 func TestMethodNotAllowedNoRoute(t *testing.T) { g := New() diff --git a/tree.go b/tree.go index 580abbaf..dc542281 100644 --- a/tree.go +++ b/tree.go @@ -418,6 +418,12 @@ type skippedNode struct { func (n *node) getValue(path string, params *Params, skippedNodes *[]skippedNode, unescape bool) (value nodeValue) { var globalParamsCount int16 + // The skippedNodes slice comes from the pooled Context with a fixed capacity and may still hold entries + // pushed by a previous getValue walk on the same Context, e.g. when handleHTTPRequest walks one method + // tree after another while collecting methods for the 405 response. Skipped nodes are only meaningful + // within a single walk, so start each walk from an empty list. + *skippedNodes = (*skippedNodes)[:0] + walk: // Outer loop for walking the tree for { prefix := n.path From 289b5a13a1ce551e115bddcf3328c91a79a80f0b Mon Sep 17 00:00:00 2001 From: paidax <2338239869@qq.com> Date: Sun, 13 Sep 2026 14:41:48 +0800 Subject: [PATCH 2/2] test(gin): cover skippedNodes reset on reused pooled Contexts The overflow reproducer only exercises one getValue walk per Context, so a regression that only manifests on the second request served from a recycled pooled Context would slip through. Drive the same #4818 route table through two consecutive requests on one router so the 405 walk re-enters getValue with reused pool state, asserting no panic and a 404 on both passes. Follow-up to the review of #4828 (points 3-4). --- gin_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/gin_test.go b/gin_test.go index 15a66748..9e1f81b8 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1073,6 +1073,37 @@ func TestMethodNotAllowedSkippedNodesOverflow(t *testing.T) { assert.Equal(t, http.StatusNotFound, resp.Code) } +// Test that the skippedNodes reset also holds on the pool-reuse path: the bug +// in #4818 was skippedNodes state leaking across getValue walks, so serve the +// same 405-triggering request twice on one router, letting the second walk run +// on a recycled pooled Context. +func TestMethodNotAllowedSkippedNodesOverflowOnContextReuse(t *testing.T) { + SetMode(TestMode) + router := New() + router.HandleMethodNotAllowed = true + + h := func(c *Context) {} + router.OPTIONS("/:p0/:p1/a/:p2", h) + router.GET("/:p0/:p1/a/:p2", h) + router.PATCH("/b/:p0/:p1/c", h) + router.DELETE("/b/:p0/:p1/d/:p3", h) + router.GET("/b/:p0/:p1/e/f", h) + router.POST("/b/:p0/:p1/g/:p4/h", h) + router.OPTIONS("/b/:p0/:p1/g/:p4/h", h) + router.DELETE("/b/cache", h) + router.GET("/b/clients/:p1/g", h) + router.POST("/b/clients/:p1/g", h) + router.PATCH("/b/clients/:p1/g/:p4", h) + router.OPTIONS("/b/clients/:p1/g/:p4", h) + + for i := 0; i < 2; i++ { + req := httptest.NewRequest(http.MethodPost, "/b/clients/42", nil) + resp := httptest.NewRecorder() + assert.NotPanics(t, func() { router.ServeHTTP(resp, req) }) + assert.Equal(t, http.StatusNotFound, resp.Code) + } +} + // Test the fix for https://github.com/gin-gonic/gin/issues/4002 func TestMethodNotAllowedNoRoute(t *testing.T) { g := New()