bytedance / go-tagexpr

An interesting go struct tag expression syntax for field validation, etc.
Apache License 2.0
1.66k stars 142 forks source link

panic when map contains nil value #9

Closed qjpcpu closed 5 years ago

qjpcpu commented 5 years ago

reproduce as code below:

type MyStruct struct {
    Data map[string]interface{}
}

func main() {
    ms := MyStruct{
        Data: map[string]interface{}{
            "a": "111",
            "b": nil,
        },
    }
    vd := validator.New("vd")
    err := vd.Validate(ms)
    fmt.Println(err)

}

stack info:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x2 pc=0x101177a]

goroutine 1 [running]:
reflect.maplen(0x2, 0x15)
        /Users/qujianping/local/go/src/runtime/map.go:1371 +0xa
reflect.Value.MapKeys(0x13b7ba0, 0xc0000aff20, 0x195, 0x14262ad, 0x1, 0x1426442)
        /Users/qujianping/local/go/src/reflect/value.go:1136 +0x208
simple/vendor/github.com/bytedance/go-tagexpr.(*TagExpr).Range(0xc00033c0c0, 0xc0002ceb40, 0x5819e00, 0x0)
        /Users/qujianping/local/src/simple/vendor/github.com/bytedance/go-tagexpr/tagexpr.go:792 +0x95c
simple/vendor/github.com/bytedance/go-tagexpr/validator.(*Validator).Validate.func1(0xc00033c0c0, 0x0, 0x0, 0x15e92e0, 0x13cefc0)
        /Users/qujianping/local/src/simple/vendor/github.com/bytedance/go-tagexpr/validator/validator.go:78 +0x18c
simple/vendor/github.com/bytedance/go-tagexpr.(*VM).subRunAll(0xc0000aff80, 0x0, 0x0, 0x0, 0x13cefc0, 0xc0000aff20, 0x19, 0xc000163ed8, 0x80, 0x13c73a0)
        /Users/qujianping/local/src/simple/vendor/github.com/bytedance/go-tagexpr/tagexpr.go:160 +0xb46
simple/vendor/github.com/bytedance/go-tagexpr.(*VM).RunAny(0xc0000aff80, 0x13cefc0, 0xc0000aff20, 0xc000163ed8, 0xc000163ec8, 0x100d509)
        /Users/qujianping/local/src/simple/vendor/github.com/bytedance/go-tagexpr/tagexpr.go:144 +0x82
simple/vendor/github.com/bytedance/go-tagexpr/validator.(*Validator).Validate(0xc000163f68, 0x13cefc0, 0xc0000aff20, 0x0, 0x0, 0x0, 0xc000163f67, 0x1)
        /Users/qujianping/local/src/simple/vendor/github.com/bytedance/go-tagexpr/validator/validator.go:70 +0x197
main.main()
        /Users/qujianping/local/src/simple/main.go:21 +0x1b0
andeya commented 5 years ago

fixed: https://github.com/bytedance/go-tagexpr/commit/2306624637880dd9fac6cfe9b07a26aaf788f75d