From 1bd86d8d48940d0919a3d47729a17520a32e05f9 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 10 Nov 2024 16:50:20 -0500 Subject: [PATCH] fix(strings): Avoid potential overflow on 32-bit systems (#2257) Signed-off-by: Dave Henderson --- internal/funcs/strings.go | 4 ++-- strings/strings.go | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/funcs/strings.go b/internal/funcs/strings.go index 44054f9b9..8b27ad19a 100644 --- a/internal/funcs/strings.go +++ b/internal/funcs/strings.go @@ -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) } @@ -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) } diff --git a/strings/strings.go b/strings/strings.go index aece28a20..007714e2b 100644 --- a/strings/strings.go +++ b/strings/strings.go @@ -3,6 +3,7 @@ package strings import ( "fmt" + "math" "regexp" "sort" "strings" @@ -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.