bug说明及分析
Gin master分支在20190602-20190612期间的版本,存在通过c.PostForm获取form中的参数有误的bug。
此bug在有一定并发时,由于复用之前的连接而未清除之前的缓存,导致获取的参数为旧参数。
type Context struct {
...
// formCache use url.ParseQuery cached PostForm contains the parsed form data from POST, PATCH,
// or PUT body parameters.
formCache url.Values
}
func (c *Context) reset() {
c.Writer = &c.writermem
c.Params = c.Params[0:0]
c.handlers = nil
c.index = -1
c.fullPath = ""
c.Keys = nil
c.Errors = c.Errors[0:0]
c.Accepted = nil
c.queryCache = nil
//此处未重置formCache
}
func (c *Context) getFormCache() {
if c.formCache == nil {
c.formCache = make(url.Values)
req := c.Request
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if err != http.ErrNotMultipart {
debugPrint("error on parse multipart form array: %v", err)
}
}
c.formCache = req.PostForm
}
}
bug修复历史
具体修复历史见:此处
建议
更新gin至最新即可,如果无法更新,则建议使用c.Request.PostFormValue(key)代替c.PostForm(key)