diff --git a/context.go b/context.go index 9be52f0aa..d81176852 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)[: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) + } +}