diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-06-03 10:35:45 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-06-03 10:35:45 +0300 |
commit | 90c774908530390daa5813fcdd31435999971359 (patch) | |
tree | 0f0d7d0967d7ef937ce51ffef29458d213595d7a /tpl/strings | |
parent | 13435a6f608306c5094fdcd72a1d9538727f91b2 (diff) | |
download | hugo-90c774908530390daa5813fcdd31435999971359.tar.gz hugo-90c774908530390daa5813fcdd31435999971359.zip |
tpl/strings: Adjust the overflow validation in strings.Repeat
This now matches the validation in the stdlib, but we return an error instead.
Diffstat (limited to 'tpl/strings')
-rw-r--r-- | tpl/strings/strings.go | 8 | ||||
-rw-r--r-- | tpl/strings/strings_test.go | 2 |
2 files changed, 5 insertions, 5 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index 796b8ff62..7bd6a9af0 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -17,7 +17,6 @@ import ( "errors" "fmt" "html/template" - "math" _strings "strings" "unicode/utf8" @@ -420,7 +419,6 @@ func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) { } // Repeat returns a new string consisting of count copies of the string s. -// The count is limited to an in16 value (up to 32767). func (ns *Namespace) Repeat(n, s interface{}) (string, error) { ss, err := cast.ToStringE(s) if err != nil { @@ -432,8 +430,10 @@ func (ns *Namespace) Repeat(n, s interface{}) (string, error) { return "", err } - if sn > math.MaxInt16 { - return "", fmt.Errorf("Cannot repeat string more than %d times", math.MaxInt16) + if sn < 0 { + return "", errors.New("strings: negative Repeat count") + } else if sn > 0 && len(ss)*sn/sn != len(ss) { + return "", errors.New("strings: Repeat count causes overflow") } return _strings.Repeat(ss, sn), nil diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go index bf19ad562..6f714702c 100644 --- a/tpl/strings/strings_test.go +++ b/tpl/strings/strings_test.go @@ -730,7 +730,7 @@ func TestRepeat(t *testing.T) { // errors {"", tstNoStringer{}, false}, {tstNoStringer{}, "", false}, - {"hi", math.MaxInt16 + 1, false}, + {"ab", math.MaxInt64, false}, } { errMsg := fmt.Sprintf("[%d] %v", i, test) |