verify that all LengthInt calls are known

There were some funcs with LengthInt calls on unknown values
This commit is contained in:
James Bardin 2018-10-18 19:19:59 -04:00
parent b3a491d035
commit 9e3c23d890
1 changed files with 18 additions and 6 deletions

View File

@ -58,6 +58,11 @@ var ElementFunc = function.New(&function.Spec{
// can't happen because we checked this in the Type function above
return cty.DynamicVal, fmt.Errorf("invalid index: %s", err)
}
if !args[0].IsKnown() {
return cty.UnknownVal(retType), nil
}
l := args[0].LengthInt()
if l == 0 {
return cty.DynamicVal, fmt.Errorf("cannot use element function with an empty list")
@ -251,6 +256,10 @@ var IndexFunc = function.New(&function.Spec{
return cty.NilVal, fmt.Errorf("argument must be a list or tuple")
}
if !args[0].IsKnown() {
return cty.UnknownVal(cty.Number), nil
}
if args[0].LengthInt() == 0 { // Easy path
return cty.NilVal, fmt.Errorf("cannot search an empty list")
}
@ -666,6 +675,9 @@ var MatchkeysFunc = function.New(&function.Spec{
return args[0].Type(), nil
},
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
if !args[0].IsKnown() {
return cty.UnknownVal(cty.List(retType.ElementType())), nil
}
if args[0].LengthInt() != args[1].LengthInt() {
return cty.ListValEmpty(retType.ElementType()), fmt.Errorf("length of keys and values should be equal")
@ -885,14 +897,14 @@ var ValuesFunc = function.New(&function.Spec{
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
mapVar := args[0]
if mapVar.LengthInt() == 0 {
return cty.ListValEmpty(retType.ElementType()), nil
}
if !mapVar.IsWhollyKnown() {
return cty.UnknownVal(retType), nil
}
if mapVar.LengthInt() == 0 {
return cty.ListValEmpty(retType.ElementType()), nil
}
keys, err := Keys(mapVar)
if err != nil {
return cty.NilVal, err
@ -941,7 +953,7 @@ var ZipmapFunc = function.New(&function.Spec{
keys := args[0]
values := args[1]
if keys.LengthInt() == 0 {
if !keys.IsKnown() || !values.IsKnown() || keys.LengthInt() == 0 {
return cty.Map(cty.DynamicPseudoType), nil
}
@ -955,7 +967,7 @@ var ZipmapFunc = function.New(&function.Spec{
keys := args[0]
values := args[1]
if keys.LengthInt() == 0 {
if !keys.IsKnown() || !values.IsKnown() || keys.LengthInt() == 0 {
return cty.MapValEmpty(cty.DynamicPseudoType), nil
}