Merge pull request #143 from hashicorp/b-invalid-object-value

Fix panic when decoding invalid value structure into struct
This commit is contained in:
Mitchell Hashimoto 2016-08-22 12:37:30 -04:00 committed by GitHub
commit bc40da04e8
3 changed files with 40 additions and 1 deletions

View File

@ -489,7 +489,7 @@ func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value)
// the yacc parser would always ensure top-level elements were arrays. The new
// parser does not make the same guarantees, thus we need to convert any
// top-level literal elements into a list.
if _, ok := node.(*ast.LiteralType); ok {
if _, ok := node.(*ast.LiteralType); ok && item != nil {
node = &ast.ObjectList{Items: []*ast.ObjectItem{item}}
}

View File

@ -328,6 +328,20 @@ func TestDecode_interface(t *testing.T) {
},
},
},
// Terraform GH-8295 sanity test that basic decoding into
// interface{} works.
{
"terraform_variable_invalid.json",
false,
map[string]interface{}{
"variable": []map[string]interface{}{
map[string]interface{}{
"whatever": "abc123",
},
},
},
},
}
for _, tc := range cases {
@ -676,6 +690,26 @@ func TestDecode_structureMap(t *testing.T) {
}
}
func TestDecode_structureMapInvalid(t *testing.T) {
// Terraform GH-8295
type hclVariable struct {
Default interface{}
Description string
Fields []string `hcl:",decodedFields"`
}
type rawConfig struct {
Variable map[string]*hclVariable
}
var actual rawConfig
err := Decode(&actual, testReadFile(t, "terraform_variable_invalid.json"))
if err == nil {
t.Fatal("expected error")
}
}
func TestDecode_interfaceNonPointer(t *testing.T) {
var value interface{}
err := Decode(value, testReadFile(t, "basic_int_string.hcl"))

View File

@ -0,0 +1,5 @@
{
"variable": {
"whatever": "abc123"
}
}