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
17 changes: 11 additions & 6 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ type RequestContext struct {
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
Errors errors.ErrorChain

Params param.Params
handlers HandlersChain
fullPath string
index int8
HTMLRender render.HTMLRender
Params param.Params
paramsCount int
handlers HandlersChain
fullPath string
index int8
HTMLRender render.HTMLRender

// This mutex protect Keys map.
mu sync.RWMutex
Expand Down Expand Up @@ -298,7 +299,7 @@ func (ctx *RequestContext) SetEnableTrace(enable bool) {
// Set the Request filed before use it for handlers
func NewContext(maxParams uint16) *RequestContext {
v := make(param.Params, 0, maxParams)
ctx := &RequestContext{Params: v, index: -1}
ctx := &RequestContext{Params: v, index: -1, paramsCount: int(maxParams)}
return ctx
}

Expand Down Expand Up @@ -845,6 +846,10 @@ func (ctx *RequestContext) HandlerName() string {
return utils.NameOfFunction(ctx.handlers.Last())
}

func (ctx *RequestContext) GetParamsCount() int {
return ctx.paramsCount
}

func (ctx *RequestContext) ResetWithoutConn() {
ctx.Params = ctx.Params[0:0]
ctx.Errors = ctx.Errors[0:0]
Expand Down
6 changes: 6 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,11 @@ 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
if cap(ctx.Params) < ctx.GetParamsCount() {
3DRX marked this conversation as resolved.
Show resolved Hide resolved
ctx.Params = make(param.Params, ctx.GetParamsCount())
}

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