diff --git a/context.go b/context.go index 9be52f0aa..e794a97de 100644 --- a/context.go +++ b/context.go @@ -152,7 +152,14 @@ 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/context_test.go b/context_test.go index 9b2b59e99..159f703be 100644 --- a/context_test.go +++ b/context_test.go @@ -1822,3 +1822,25 @@ 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"}, + }) + // 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) + + 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) + } +} 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()))