Closed
Description
go-zero/rest/httpx/requests.go
Lines 134 to 139 in 4307ce4
由于 atomic.Value
Strore 方法会 校验和首次存储的数据类型是否一致,此处存在 panic 风险。
复现代码:
package main
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
)
func main() {
httpx.SetValidator(Valid1{})
httpx.SetValidator(Valid2{}) // panic: sync/atomic: store of inconsistently typed value into Value
}
type Valid1 struct{}
func (v Valid1) Validate(*http.Request, any) error { return nil }
type Valid2 struct{}
func (v Valid2) Validate(*http.Request, any) error { return nil }
SetValidator
方法改进建议:
func SetValidator(val Validator) {
validator.Store(&validate{val: val})
}
type validate struct {
val Validator
}
func (v *validate) Validate(r *http.Request, data any) error {
if v.val != nil {
return v.val.Validate(r, data)
}
return nil
}
Metadata
Assignees
Labels
No labels