Skip to content

Commit

Permalink
stdlib: Don't panic if flatten finds a null list/set/tuple value
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin authored Aug 22, 2022
1 parent 834994b commit 4d209ea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cty/function/stdlib/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
if len(flattenListMarks) > 0 {
markses = append(markses, flattenListMarks)
}

if !flattenList.Length().IsKnown() {
// If we don't know the length of what we're flattening then we can't
// predict the length of our result yet either.
Expand All @@ -542,7 +543,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
isKnown = false
}

if val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType() {
if !val.IsNull() && (val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType()) {
if !val.IsKnown() {
isKnown = false
_, unknownMarks := val.Unmark()
Expand Down
104 changes: 104 additions & 0 deletions cty/function/stdlib/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,110 @@ func TestFlatten(t *testing.T) {
cty.UnknownVal(cty.DynamicPseudoType),
"",
},
{
// null of an unknown type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
"",
},
{
// null of a string type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
"",
},
{
// null of a list type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
"",
},
{
// null of a tuple type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
"",
},
{
// nested null of an unknown type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
"",
},
{
// nested null of a string type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
"",
},
{
// nested null of a list type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
"",
},
{
// nested null of a tuple type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
"",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 4d209ea

Please sign in to comment.