Skip to content

Commit

Permalink
Fix: Unexpected panic from Convert while dstPrt is a pointer to a nil…
Browse files Browse the repository at this point in the history
… pointer.
  • Loading branch information
cmstar committed Sep 18, 2023
1 parent 6cbd57a commit d4e210d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ func (c *Conv) Convert(src interface{}, dstPtr interface{}) error {

for dstValue.Kind() == reflect.Ptr {
dstValue = dstValue.Elem()
if dstValue.Kind() == reflect.Invalid {
panic(errForFunction(fnName, "the underlying pointer must be initialized"))
}
}

dstTyp := dstValue.Type()
Expand Down
17 changes: 17 additions & 0 deletions conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,23 @@ func TestConv_Convert_panic(t *testing.T) {
var p *int
_defaultConv.Convert("", p)
})

t.Run("ptr-to-nil-ptr", func(t *testing.T) {
defer func() {
var err interface{}
if err = recover(); err == nil {
t.Fatalf("should panic an error")
}

const wantMsg = "conv.Convert: the underlying pointer must be initialized"
if err.(error).Error() != wantMsg {
t.Fatalf("should panic an error with message: '%v', got '%v'", wantMsg, err)
}
}()

var p *int
_defaultConv.Convert("", &p)
})
}

func TestConv_Convert_ptr(t *testing.T) {
Expand Down

0 comments on commit d4e210d

Please sign in to comment.