Skip to content

Commit

Permalink
convert: Don't modify the captured element type in a collection conve…
Browse files Browse the repository at this point in the history
…rsion

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.
  • Loading branch information
apparentlymart committed Mar 16, 2021
1 parent 34f2ef6 commit d1aeddc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cty/convert/conversion_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d1aeddc

Please sign in to comment.