From 44a99e0ae51a03ea8e447445871815b9665b185b Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 9 May 2017 20:45:53 +0200 Subject: [PATCH] core: Avoid crash on empty TypeSet blocks (#14305) --- helper/schema/serialize.go | 3 +++ helper/schema/set_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/helper/schema/serialize.go b/helper/schema/serialize.go index 3eb2d0075..fe6d7504c 100644 --- a/helper/schema/serialize.go +++ b/helper/schema/serialize.go @@ -85,6 +85,9 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) { // to hash complex substructures when used in sets, and so the serialization // is not reversible. func SerializeResourceForHash(buf *bytes.Buffer, val interface{}, resource *Resource) { + if val == nil { + return + } sm := resource.Schema m := val.(map[string]interface{}) var keys []string diff --git a/helper/schema/set_test.go b/helper/schema/set_test.go index 87a9f7228..21f292954 100644 --- a/helper/schema/set_test.go +++ b/helper/schema/set_test.go @@ -111,3 +111,20 @@ func TestSetUnion(t *testing.T) { func testSetInt(v interface{}) int { return v.(int) } + +func TestHashResource_nil(t *testing.T) { + resource := &Resource{ + Schema: map[string]*Schema{ + "name": { + Type: TypeString, + Optional: true, + }, + }, + } + f := HashResource(resource) + + idx := f(nil) + if idx != 0 { + t.Fatalf("Expected 0 when hashing nil, given: %d", idx) + } +}