From dcac7db0f0b4a47d91e57be5f1723e1f4481b711 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 16 Nov 2014 13:52:37 -0600 Subject: [PATCH] Fix CopyExact to work on a source string --- util/copy.go | 2 +- util/copy_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/util/copy.go b/util/copy.go index c7d64cc2..793a99cd 100644 --- a/util/copy.go +++ b/util/copy.go @@ -15,7 +15,7 @@ func CopyExact(dest interface{}, src interface{}) { panic(fmt.Sprintf("dest not addressable: %T", dest)) } if sV.Kind() == reflect.String { - sV = sV.Convert(dV.Type()) + sV = sV.Convert(reflect.SliceOf(dV.Type().Elem())) } if dV.Len() != sV.Len() { panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len())) diff --git a/util/copy_test.go b/util/copy_test.go index 450e5c4b..284840a3 100644 --- a/util/copy_test.go +++ b/util/copy_test.go @@ -44,3 +44,18 @@ func TestCopyLenMismatch(t *testing.T) { }() CopyExact(make([]byte, 2), "abc") } + +func TestCopySrcString(t *testing.T) { + dest := make([]byte, 3) + CopyExact(dest, "lol") + if string(dest) != "lol" { + t.FailNow() + } + defer func() { + r := recover() + if r == nil { + t.FailNow() + } + }() + CopyExact(dest, "rofl") +} -- 2.48.1