fix a crash if a non-pointer is given to Decode

/cc @sethvargo
This commit is contained in:
Mitchell Hashimoto 2014-10-09 20:57:32 -07:00
parent cd87a48c3c
commit 09d7815762
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package hcl package hcl
import ( import (
"errors"
"fmt" "fmt"
"reflect" "reflect"
"sort" "sort"
@ -27,8 +28,13 @@ func Decode(out interface{}, in string) error {
// DecodeObject is a lower-level version of Decode. It decodes a // DecodeObject is a lower-level version of Decode. It decodes a
// raw Object into the given output. // raw Object into the given output.
func DecodeObject(out interface{}, n *hcl.Object) error { func DecodeObject(out interface{}, n *hcl.Object) error {
val := reflect.ValueOf(out)
if val.Kind() != reflect.Ptr {
return errors.New("result must be a pointer")
}
var d decoder var d decoder
return d.decode("root", n, reflect.ValueOf(out).Elem()) return d.decode("root", n, val.Elem())
} }
type decoder struct { type decoder struct {

View File

@ -425,6 +425,14 @@ func TestDecode_structureMap(t *testing.T) {
} }
} }
func TestDecode_interfaceNonPointer(t *testing.T) {
var value interface{}
err := Decode(value, testReadFile(t, "basic_int_string.hcl"))
if err == nil {
t.Fatal("should error")
}
}
func TestDecode_intString(t *testing.T) { func TestDecode_intString(t *testing.T) {
var value struct { var value struct {
Count int Count int