Skip to content

Commit

Permalink
function/stdlib: "Substr" returns zero length string when length is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Spain authored Mar 9, 2020
1 parent 440c597 commit 9ae7dd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cty/function/stdlib/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions cty/function/stdlib/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9ae7dd9

Please sign in to comment.