From 6680b1f16b88cf2821407389e4a1403e0ce0ac76 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 12 Dec 2017 18:18:38 +0100 Subject: [PATCH] core: check for negative indices in ResourceConfig.get The bounds checking in ResourceConfig.get() was insufficient: it detected when the index was greater than or equal to cv.Len() but not when the index was less than zero. If the user provided an (invalid) configuration that referenced "foo.-1.bar", the provider would panic. Now it behaves the same way as if the index were too high. --- terraform/resource.go | 2 +- terraform/resource_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/terraform/resource.go b/terraform/resource.go index a8cd8dd9f0..2f5ebb5e72 100644 --- a/terraform/resource.go +++ b/terraform/resource.go @@ -346,7 +346,7 @@ func (c *ResourceConfig) get( if err != nil { return nil, false } - if i >= int64(cv.Len()) { + if int(i) < 0 || int(i) >= cv.Len() { return nil, false } current = cv.Index(int(i)).Interface() diff --git a/terraform/resource_test.go b/terraform/resource_test.go index 31d511e5c5..4566492dc3 100644 --- a/terraform/resource_test.go +++ b/terraform/resource_test.go @@ -158,6 +158,14 @@ func TestResourceConfigGet(t *testing.T) { Value: nil, }, + { + Config: map[string]interface{}{ + "foo": []interface{}{1, 2, 5}, + }, + Key: "foo.-1", + Value: nil, + }, + // get from map { Config: map[string]interface{}{