From fde3a7f74f4bd8602daeba0359ceedd4fe54a367 Mon Sep 17 00:00:00 2001 From: BrunoF2P Date: Tue, 24 Nov 2026 00:17:39 -0300 Subject: [PATCH 1/3] fix(context): clear pathValues backing array in Context.Reset() --- context.go | 1 + context_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/context.go b/context.go index 9be52f0aa..7f4164ac3 100644 --- a/context.go +++ b/context.go @@ -153,6 +153,7 @@ func (c *Context) Reset(r *http.Request, w http.ResponseWriter) { c.dsw = delayedStatusWriter{} c.path = "" // NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times + clear(*c.pathValues) *c.pathValues = (*c.pathValues)[:0] } diff --git a/context_test.go b/context_test.go index 9b2b59e99..02316e770 100644 --- a/context_test.go +++ b/context_test.go @@ -1822,3 +1822,21 @@ func TestRouteInfo(t *testing.T) { orgRI.Name = "changed" assert.NotEqual(t, expect, c.RouteInfo()) } + +func TestContextResetClearsPathValuesBackingArray(t *testing.T) { + e := New() + c := e.NewContext(nil, nil) + c.SetPathValues(PathValues{ + {Name: "id", Value: "12345"}, + {Name: "token", Value: "secret-value"}, + }) + + origCap := cap(*c.pathValues) + c.Reset(nil, nil) + + assert.Empty(t, *c.pathValues) + assert.Equal(t, origCap, cap(*c.pathValues)) + for i, pv := range (*c.pathValues)[:cap(*c.pathValues)] { + assert.Equal(t, PathValue{}, pv, "pathValues backing array element %d should be zeroed", i) + } +} From 70794f4a6b1a0747597c47d48cbd310ae6e63e09 Mon Sep 17 00:00:00 2001 From: BrunoF2P Date: Tue, 24 Nov 2026 00:39:44 -0300 Subject: [PATCH 2/3] fix(context): clear pathValues through full slice capacity --- context.go | 2 +- context_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 7f4164ac3..d81176852 100644 --- a/context.go +++ b/context.go @@ -153,7 +153,7 @@ func (c *Context) Reset(r *http.Request, w http.ResponseWriter) { c.dsw = delayedStatusWriter{} c.path = "" // NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times - clear(*c.pathValues) + clear((*c.pathValues)[:cap(*c.pathValues)]) *c.pathValues = (*c.pathValues)[:0] } diff --git a/context_test.go b/context_test.go index 02316e770..159f703be 100644 --- a/context_test.go +++ b/context_test.go @@ -1830,6 +1830,10 @@ func TestContextResetClearsPathValuesBackingArray(t *testing.T) { {Name: "id", Value: "12345"}, {Name: "token", Value: "secret-value"}, }) + // Shortening the slice leaves the previous second element in spare capacity. + c.SetPathValues(PathValues{ + {Name: "id", Value: "67890"}, + }) origCap := cap(*c.pathValues) c.Reset(nil, nil) From ac6994c6566ec0740821ffed9c316acd0bf6a3b5 Mon Sep 17 00:00:00 2001 From: BrunoF2P Date: Mon, 30 Nov 2026 19:00:32 -0300 Subject: [PATCH 3/3] fix(echo): clear pathValues before returning Context to pool in serveHTTP --- context.go | 8 +++++++- echo.go | 6 +++++- echo_test.go | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index d81176852..e794a97de 100644 --- a/context.go +++ b/context.go @@ -152,7 +152,13 @@ func (c *Context) Reset(r *http.Request, w http.ResponseWriter) { c.handler = nil c.dsw = delayedStatusWriter{} c.path = "" - // NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times + c.clearPathValues() +} + +// clearPathValues empties pathValues and zeroes its whole backing array, so a pooled Context does not keep +// the previous request's path parameter strings alive. +// NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times +func (c *Context) clearPathValues() { clear((*c.pathValues)[:cap(*c.pathValues)]) *c.pathValues = (*c.pathValues)[:0] } diff --git a/echo.go b/echo.go index adc76adf1..8a00a4824 100644 --- a/echo.go +++ b/echo.go @@ -863,7 +863,11 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { // serveHTTP implements `http.Handler` interface, which serves HTTP requests. func (e *Echo) serveHTTP(w http.ResponseWriter, r *http.Request) { c := e.contextPool.Get().(*Context) - defer e.contextPool.Put(c) + defer func() { + // Drop the request's path values before pooling so an idle Context does not keep them alive. + c.clearPathValues() + e.contextPool.Put(c) + }() c.Reset(r, w) diff --git a/echo_test.go b/echo_test.go index ecfe818c5..b179996e8 100644 --- a/echo_test.go +++ b/echo_test.go @@ -1279,6 +1279,23 @@ func TestEchoContext(t *testing.T) { e.ReleaseContext(c) } +func TestEchoServeHTTPClearsPathValuesBeforePooling(t *testing.T) { + e := New() + var pooled *Context + e.GET("/a/:id/:token", func(c *Context) error { + pooled = c + return c.NoContent(http.StatusOK) + }) + + e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/a/1/secret", nil)) + + // pooled now sits in the pool; it must not keep the request's path values alive. + assert.Empty(t, *pooled.pathValues) + for i, pv := range (*pooled.pathValues)[:cap(*pooled.pathValues)] { + assert.Equal(t, PathValue{}, pv, "pathValues backing array element %d should be zeroed", i) + } +} + func TestPreMiddlewares(t *testing.T) { e := New() assert.Equal(t, 0, len(e.PreMiddlewares()))