Skip to content

Commit 1bd86d8

Browse files
fix(strings): Avoid potential overflow on 32-bit systems (#2257)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
1 parent 94716fc commit 1bd86d8

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

internal/funcs/strings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func (StringFuncs) WordWrap(args ...interface{}) (string, error) {
376376
return "", fmt.Errorf("expected width to be a number: %w", err)
377377
}
378378

379-
if n > math.MaxUint32 {
379+
if n > math.MaxInt {
380380
return "", fmt.Errorf("width too large: %d", n)
381381
}
382382

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

394-
if n > math.MaxUint32 {
394+
if n > math.MaxInt {
395395
return "", fmt.Errorf("width too large: %d", n)
396396
}
397397

strings/strings.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package strings
33

44
import (
55
"fmt"
6+
"math"
67
"regexp"
78
"sort"
89
"strings"
@@ -129,7 +130,15 @@ func wwDefaults(opts WordWrapOpts) WordWrapOpts {
129130
// width.
130131
func WordWrap(in string, opts WordWrapOpts) string {
131132
opts = wwDefaults(opts)
132-
return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false)
133+
134+
// if we're on a 32-bit system, we need to check for overflow. If the width
135+
// is greater than maxint, we'll just use maxint.
136+
width := int(opts.Width)
137+
if width == -1 {
138+
width = int(math.MaxInt)
139+
}
140+
141+
return goutils.WrapCustom(in, width, opts.LBSeq, false)
133142
}
134143

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

0 commit comments

Comments
 (0)