Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hertz panic when edit ctx.Params on HandleFunc #1150

Merged
merged 10 commits into from
Jul 17, 2024
7 changes: 7 additions & 0 deletions pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
"github.com/cloudwego/hertz/pkg/protocol/http1"
"github.com/cloudwego/hertz/pkg/protocol/http1/factory"
"github.com/cloudwego/hertz/pkg/protocol/suite"
"github.com/cloudwego/hertz/pkg/route/param"
)

const unknownTransporterName = "unknown"
Expand Down Expand Up @@ -749,6 +750,12 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) {
return
}

// if ctx.Params is re-assigned by user in HandlerFunc and the capacity changed we need to realloc
paramsCnt := int(engine.maxParams)
3DRX marked this conversation as resolved.
Show resolved Hide resolved
if cap(ctx.Params) < paramsCnt {
ctx.Params = make(param.Params, paramsCnt)
3DRX marked this conversation as resolved.
Show resolved Hide resolved
}

// Find root of the tree for the given HTTP method
t := engine.trees
paramsPointer := &ctx.Params
Expand Down
30 changes: 30 additions & 0 deletions pkg/route/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,33 @@ func TestAcquireHijackConn(t *testing.T) {
assert.DeepEqual(t, engine, hijackConn.e)
assert.DeepEqual(t, conn, hijackConn.Conn)
}

func TestHandleParamsReassignInHandleFunc(t *testing.T) {
e := NewEngine(config.NewOptions(nil))
routes := []string{
"/:a/:b/:c",
}
for _, r := range routes {
e.GET(r, func(c context.Context, ctx *app.RequestContext) {
ctx.Params = make([]param.Param, 1)
ctx.String(consts.StatusOK, "")
})
}
testRoutes := []string{
"/aaa/bbb/ccc",
"/asd/alskja/alkdjad",
"/asd/alskja/alkdjad",
"/asd/alskja/alkdjad",
"/asd/alskja/alkdjad",
"/alksjdlakjd/ooo/askda",
"/alksjdlakjd/ooo/askda",
"/alksjdlakjd/ooo/askda",
}
ctx := e.ctxPool.Get().(*app.RequestContext)
for _, tr := range testRoutes {
r := protocol.NewRequest(http.MethodGet, tr, nil)
r.CopyTo(&ctx.Request)
e.ServeHTTP(context.Background(), ctx)
ctx.ResetWithoutConn()
}
}
Loading