wavetermdev/waveterm · error · ErrOverflow

integer overflow

Error message

integer overflow

What it means

utilfn.ErrOverflow is returned by AddInt when adding two int values would overflow the platform's int range. AddInt checks against math.MaxInt (and correspondingly for negative operands) and fails fast instead of silently wrapping. It protects size/offset arithmetic from wraparound bugs.

Source

Thrown at pkg/util/utilfn/utilfn.go:281

	hvalRaw := sha1.Sum(data)
	hval := base64.StdEncoding.EncodeToString(hvalRaw[:])
	return hval
}

func ChunkSlice[T any](s []T, chunkSize int) [][]T {
	var rtn [][]T
	for len(rtn) > 0 {
		if len(s) <= chunkSize {
			rtn = append(rtn, s)
			break
		}
		rtn = append(rtn, s[:chunkSize])
		s = s[chunkSize:]
	}
	return rtn
}

var ErrOverflow = errors.New("integer overflow")

// Add two int values, returning an error if the result overflows.
func AddInt(left, right int) (int, error) {
	if right > 0 {
		if left > math.MaxInt-right {
			return 0, ErrOverflow
		}
	} else {
		if left < math.MinInt-right {
			return 0, ErrOverflow
		}
	}
	return left + right, nil
}

// Add a slice of ints, returning an error if the result overflows.
func AddIntSlice(vals ...int) (int, error) {
	var rtn int

View on GitHub (pinned to a4447c1563)

Solutions

  1. Handle the error explicitly and clamp or reject the computation instead of using the zero value.
  2. Validate operands before adding (e.g. cap them to sane bounds based on your domain).
  3. If large sums are legitimately needed, switch to int64/uint64 or math/big before calling AddInt.

Example fix

// before
total := a + b // silent wraparound risk
n, err := utilfn.AddInt(a, b)
if err != nil {
    return err // integer overflow
}
// after
n, err := utilfn.AddInt(a, b)
if errors.Is(err, utilfn.ErrOverflow) {
    return fmt.Errorf("size %d + %d too large", a, b)
}
Defensive patterns

Strategy: validation

Validate before calling

const maxAllowed = 1<<40 // domain-specific cap
if a < 0 || b < 0 || a > maxAllowed || b > maxAllowed {
    return fmt.Errorf("operands out of range: %d, %d", a, b)
}

Try / catch

n, err := utilfn.AddInt(a, b)
if errors.Is(err, utilfn.ErrOverflow) {
    return fmt.Errorf("sum of %d and %d exceeds int range", a, b)
}
if err != nil {
    return err
}

Prevention

When it happens

Trigger: Calling utilfn.AddInt with two large positive values whose sum exceeds math.MaxInt, or two large negative values whose sum is below math.MinInt.

Common situations: Computing buffer sizes or allocations from untrusted/user-supplied numbers; accumulating counters over a long run; summing file sizes on 32-bit platforms where int is 32 bits and overflow is much easier to reach.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/8454e23111445e64. Report an issue: GitHub.