From d1aeddcd269e31952a119db5445ea19e4bcebc0f Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 16 Mar 2021 11:50:50 -0700 Subject: [PATCH] convert: Don't modify the captured element type in a collection conversion The conversion constructor functions capture their arguments into a closure to return, but some of them were then incorrectly modifying that shared variable, which meant that the result of modifications made by one call would be seen by subsequent calls, causing different (and possibly totally incorrect) behavior. We'll now directly return a replacement result type rather than modifying the variable containing the requested type. --- cty/convert/conversion_collection.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cty/convert/conversion_collection.go b/cty/convert/conversion_collection.go index 923a98ea..e70b0184 100644 --- a/cty/convert/conversion_collection.go +++ b/cty/convert/conversion_collection.go @@ -45,8 +45,10 @@ func conversionCollectionToList(ety cty.Type, conv conversion) conversion { } if len(elems) == 0 { + // Prefer a concrete type over a dynamic type when returning an + // empty list if ety == cty.DynamicPseudoType { - ety = val.Type().ElementType() + return cty.ListValEmpty(val.Type().ElementType()), nil } return cty.ListValEmpty(ety), nil } @@ -95,7 +97,7 @@ func conversionCollectionToSet(ety cty.Type, conv conversion) conversion { // Prefer a concrete type over a dynamic type when returning an // empty set if ety == cty.DynamicPseudoType { - ety = val.Type().ElementType() + return cty.SetValEmpty(val.Type().ElementType()), nil } return cty.SetValEmpty(ety), nil } @@ -148,7 +150,7 @@ func conversionCollectionToMap(ety cty.Type, conv conversion) conversion { // Prefer a concrete type over a dynamic type when returning an // empty map if ety == cty.DynamicPseudoType { - ety = val.Type().ElementType() + return cty.MapValEmpty(val.Type().ElementType()), nil } return cty.MapValEmpty(ety), nil }