Merge 289b5a13a1ce551e115bddcf3328c91a79a80f0b into b52df1fe77734c691acc1fce21647fe0ca955201

This commit is contained in:
toolsfox 2026-09-16 21:27:15 +08:00 committed by GitHub
commit cc3e0549cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 0 deletions

View File

@ -1047,6 +1047,63 @@ 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 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()

View File

@ -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