Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(strings): Avoid potential overflow on 32-bit systems #2257

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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