diff --git a/cty/function/stdlib/string.go b/cty/function/stdlib/string.go index d0b0e224..01ebc47f 100644 --- a/cty/function/stdlib/string.go +++ b/cty/function/stdlib/string.go @@ -144,8 +144,14 @@ var SubstrFunc = function.New(&function.Spec{ } offset += totalLen + } else if length == 0 { + // Short circuit here, after error checks, because if a + // string of length 0 has been requested it will always + // be the empty string + return cty.StringVal(""), nil } + sub := in pos := 0 var i int diff --git a/cty/function/stdlib/string_test.go b/cty/function/stdlib/string_test.go index 701cd758..234386cf 100644 --- a/cty/function/stdlib/string_test.go +++ b/cty/function/stdlib/string_test.go @@ -323,6 +323,12 @@ func TestSubstr(t *testing.T) { cty.NumberIntVal(10), cty.StringVal(""), }, + { + cty.StringVal("hello"), + cty.NumberIntVal(0), + cty.NumberIntVal(0), + cty.StringVal(""), + }, { cty.StringVal("noël"), cty.NumberIntVal(0),