From 3c41a7ca1ef5aeb6370c3e339136fcd32ad3a115 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 7 Mar 2017 15:01:29 -0500 Subject: [PATCH 1/2] Add test for Validate crash Crash during Validate walk with nested variable default. --- config/config_test.go | 6 ++++++ config/test-fixtures/validate-var-nested/main.tf | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 config/test-fixtures/validate-var-nested/main.tf diff --git a/config/config_test.go b/config/config_test.go index 2ef68dae9..3c7828b6a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -201,6 +201,12 @@ func TestConfigValidate_table(t *testing.T) { true, "cannot contain interp", }, + { + "nested types in variable default", + "validate-var-nested", + true, + "ERROR", + }, } for i, tc := range cases { diff --git a/config/test-fixtures/validate-var-nested/main.tf b/config/test-fixtures/validate-var-nested/main.tf new file mode 100644 index 000000000..a3d64647b --- /dev/null +++ b/config/test-fixtures/validate-var-nested/main.tf @@ -0,0 +1,6 @@ +variable "foo" { + default = [["foo", "bar"]] +} +variable "bar" { + default = [{foo = "bar"}] +} From a11163590818587e5f17f9467688f6ebca4d75cf Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 7 Mar 2017 15:33:39 -0500 Subject: [PATCH 2/2] Fix panic in interpolate_walk Verify that we have enough containers in the stack to look for a map in replaceCurrent. --- config/config_test.go | 4 ++-- config/interpolate_walk.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 3c7828b6a..b391295c8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -204,8 +204,8 @@ func TestConfigValidate_table(t *testing.T) { { "nested types in variable default", "validate-var-nested", - true, - "ERROR", + false, + "", }, } diff --git a/config/interpolate_walk.go b/config/interpolate_walk.go index 81fa81208..ead3d102e 100644 --- a/config/interpolate_walk.go +++ b/config/interpolate_walk.go @@ -206,6 +206,12 @@ func (w *interpolationWalker) Primitive(v reflect.Value) error { } func (w *interpolationWalker) replaceCurrent(v reflect.Value) { + // if we don't have at least 2 values, we're not going to find a map, but + // we could panic. + if len(w.cs) < 2 { + return + } + c := w.cs[len(w.cs)-2] switch c.Kind() { case reflect.Map: