Skip to content

Commit

Permalink
fix(strings): Avoid potential overflow on 32-bit systems (#2257)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson authored Nov 10, 2024
1 parent 94716fc commit 1bd86d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (StringFuncs) WordWrap(args ...interface{}) (string, error) {
return "", fmt.Errorf("expected width to be a number: %w", err)
}

if n > math.MaxUint32 {
if n > math.MaxInt {
return "", fmt.Errorf("width too large: %d", n)
}

Expand All @@ -391,7 +391,7 @@ func (StringFuncs) WordWrap(args ...interface{}) (string, error) {
return "", fmt.Errorf("expected width to be a number: %w", err)
}

if n > math.MaxUint32 {
if n > math.MaxInt {
return "", fmt.Errorf("width too large: %d", n)
}

Expand Down
11 changes: 10 additions & 1 deletion strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strings

import (
"fmt"
"math"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -129,7 +130,15 @@ func wwDefaults(opts WordWrapOpts) WordWrapOpts {
// width.
func WordWrap(in string, opts WordWrapOpts) string {
opts = wwDefaults(opts)
return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false)

// if we're on a 32-bit system, we need to check for overflow. If the width
// is greater than maxint, we'll just use maxint.
width := int(opts.Width)
if width == -1 {
width = int(math.MaxInt)
}

return goutils.WrapCustom(in, width, opts.LBSeq, false)
}

// SkipLines - skip the given number of lines (ending with \n) from the string.
Expand Down

0 comments on commit 1bd86d8

Please sign in to comment.