terraform: detect null values in for_each sets

Previously, passing `[null, null]` to `for_each` caused a panic. This
commit detects this invalid usage and returns an error instead.

Fixes #24047
This commit is contained in:
Alisdair McDiarmid 2020-02-14 17:20:08 -05:00
parent 678760b61a
commit 0ef7d6dea7
1 changed files with 16 additions and 0 deletions

View File

@ -89,6 +89,22 @@ func evaluateResourceForEachExpressionKnown(expr hcl.Expression, ctx EvalContext
if !forEachVal.IsWhollyKnown() {
return map[string]cty.Value{}, false, diags
}
// A set of strings may contain null, which makes it impossible to
// convert to a map, so we must return an error
it := forEachVal.ElementIterator()
for it.Next() {
item, _ := it.Element()
if item.IsNull() {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid for_each set argument",
Detail: fmt.Sprintf(`The given "for_each" argument value is unsuitable: "for_each" sets must not contain null values.`),
Subject: expr.Range().Ptr(),
})
return nil, true, diags
}
}
}
return forEachVal.AsValueMap(), true, nil