From 5f175bf3fc9be54b5711509f321ee4d2e21baed2 Mon Sep 17 00:00:00 2001 From: kcln1687 Date: Tue, 19 Jul 2016 21:20:03 +0800 Subject: [PATCH] fix: panic when passing pointer to pointer as param --- structs.go | 2 +- structs_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/structs.go b/structs.go index 1c05275..bfcfb70 100644 --- a/structs.go +++ b/structs.go @@ -431,7 +431,7 @@ func strctVal(s interface{}) reflect.Value { v := reflect.ValueOf(s) // if pointer get the underlying element≤ - if v.Kind() == reflect.Ptr { + for v.Kind() == reflect.Ptr { v = v.Elem() } diff --git a/structs_test.go b/structs_test.go index 07c23db..69a3943 100644 --- a/structs_test.go +++ b/structs_test.go @@ -1376,3 +1376,24 @@ func TestMap_InterfaceValue(t *testing.T) { t.Errorf("Value does not match expected: %q != %q", s["A"], expected) } } + +func TestPointer2Pointer(t *testing.T) { + defer func() { + err := recover() + if err != nil { + fmt.Printf("err %+v\n", err) + t.Error("Internal nil pointer should not panic") + } + }() + a := &Animal{ + Name: "Fluff", + Age: 4, + } + _ = Map(&a) + + b := &a + _ = Map(&b) + + c := &b + _ = Map(&c) +}