{"record":{"id":"8454e23111445e64","repo":"wavetermdev/waveterm","slug":"integer-overflow","errorCode":null,"errorMessage":"integer overflow","messagePattern":"integer overflow","errorType":"error_code","errorClass":"ErrOverflow","httpStatus":null,"severity":"error","filePath":"pkg/util/utilfn/utilfn.go","lineNumber":281,"sourceCode":"\thvalRaw := sha1.Sum(data)\n\thval := base64.StdEncoding.EncodeToString(hvalRaw[:])\n\treturn hval\n}\n\nfunc ChunkSlice[T any](s []T, chunkSize int) [][]T {\n\tvar rtn [][]T\n\tfor len(rtn) > 0 {\n\t\tif len(s) <= chunkSize {\n\t\t\trtn = append(rtn, s)\n\t\t\tbreak\n\t\t}\n\t\trtn = append(rtn, s[:chunkSize])\n\t\ts = s[chunkSize:]\n\t}\n\treturn rtn\n}\n\nvar ErrOverflow = errors.New(\"integer overflow\")\n\n// Add two int values, returning an error if the result overflows.\nfunc AddInt(left, right int) (int, error) {\n\tif right > 0 {\n\t\tif left > math.MaxInt-right {\n\t\t\treturn 0, ErrOverflow\n\t\t}\n\t} else {\n\t\tif left < math.MinInt-right {\n\t\t\treturn 0, ErrOverflow\n\t\t}\n\t}\n\treturn left + right, nil\n}\n\n// Add a slice of ints, returning an error if the result overflows.\nfunc AddIntSlice(vals ...int) (int, error) {\n\tvar rtn int","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/util/utilfn/utilfn.go#L263-L299","documentation":"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.","triggerScenarios":"Calling utilfn.AddInt with two large positive values whose sum exceeds math.MaxInt, or two large negative values whose sum is below math.MinInt.","commonSituations":"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.","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."],"exampleFix":"// before\ntotal := a + b // silent wraparound risk\nn, err := utilfn.AddInt(a, b)\nif err != nil {\n    return err // integer overflow\n}\n// after\nn, err := utilfn.AddInt(a, b)\nif errors.Is(err, utilfn.ErrOverflow) {\n    return fmt.Errorf(\"size %d + %d too large\", a, b)\n}","handlingStrategy":"validation","validationCode":"const maxAllowed = 1<<40 // domain-specific cap\nif a < 0 || b < 0 || a > maxAllowed || b > maxAllowed {\n    return fmt.Errorf(\"operands out of range: %d, %d\", a, b)\n}","typeGuard":null,"tryCatchPattern":"n, err := utilfn.AddInt(a, b)\nif errors.Is(err, utilfn.ErrOverflow) {\n    return fmt.Errorf(\"sum of %d and %d exceeds int range\", a, b)\n}\nif err != nil {\n    return err\n}","preventionTips":["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."],"tags":["go","arithmetic","integer-overflow","input-validation"],"backgroundTag":"integer-overflow","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}