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 intView on GitHub (pinned to a4447c1563)
Solutions
- Handle the error explicitly and clamp or reject the computation instead of using the zero value.
- Validate operands before adding (e.g. cap them to sane bounds based on your domain).
- 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
- Never ignore the error from AddInt — the int return is 0 on overflow, not the wrapped sum.
- Bound user-supplied numbers to domain-specific maximums before arithmetic.
- Prefer int64 for size/offset math on 32-bit targets where int overflow is easier to hit.
- Add unit tests with math.MaxInt operands for any summing helper.
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
- invalid destination path: %w
- beginning of file
- procinfo: process not found
- bind tags must be self closing
- doctype not supported
AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01).
Data as JSON: /api/errors/8454e23111445e64.
Report an issue: GitHub.