Skip to content

Commit c8a98d0

Browse files
committed
fix: Don't panic when vm.Run method accepts nil parameter
Change-Id: Iacd7d76eb6b4713ae96de7e05bd855972e0c423d
1 parent 29ec1fb commit c8a98d0

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

tagexpr.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ func (vm *VM) MustWarmUp(structOrStructPtr ...interface{}) {
9393
// If the structure type has not been warmed up,
9494
// it will be slower when it is first called.
9595
func (vm *VM) Run(structOrStructPtr interface{}) (*TagExpr, error) {
96-
if structOrStructPtr == nil {
97-
return nil, errors.New("cannot run nil interface")
96+
u := tpack.Unpack(structOrStructPtr)
97+
if u.IsNil() {
98+
return nil, errors.New("cannot run nil data")
9899
}
99-
u := tpack.Unpack(structOrStructPtr).UnderlyingElem()
100+
u = u.UnderlyingElem()
100101
tid := u.RuntimeTypeID()
101102
var err error
102103
vm.rw.RLock()
@@ -276,7 +277,7 @@ func (f *fieldVM) packElemFrom(ptr uintptr) reflect.Value {
276277
func (s *structVM) setIfaceTagExprGetter(f *fieldVM) {
277278
s.ifaceTagExprGetters = append(s.ifaceTagExprGetters, func(ptr uintptr) (*TagExpr, bool) {
278279
v := f.packElemFrom(ptr)
279-
if !v.IsValid() {
280+
if !v.IsValid() || v.IsNil() {
280281
return nil, false
281282
}
282283
te, ok := s.vm.runFromValue(v)

validator/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func Example() {
8989
f := &F{}
9090
f.f.g = 10
9191
fmt.Println(vd.Validate(f))
92+
f = nil
93+
fmt.Println(vd.Validate(f))
9294

9395
// Output:
9496
// true
@@ -98,6 +100,7 @@ func Example() {
98100
// invalid d: [x y]
99101
// invalid parameter: e
100102
// {"succ":false, "error":"invalid parameter: f.g"}
103+
// cannot run nil data
101104
}
102105
```
103106

validator/example_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func Example() {
7171
f := &F{}
7272
f.f.g = 10
7373
fmt.Println(vd.Validate(f))
74+
f = nil
75+
fmt.Println(vd.Validate(f))
7476

7577
// Output:
7678
// true
@@ -80,4 +82,5 @@ func Example() {
8082
// invalid d: [x y]
8183
// invalid parameter: e
8284
// {"succ":false, "error":"invalid parameter: f.g"}
85+
// cannot run nil data
8386
}

0 commit comments

Comments
 (0)