golang/go · error
bytes: negative Repeat count
Error message
bytes: negative Repeat count
What it means
bytes.Repeat returns count copies of b. It panics with "bytes: negative Repeat count" when count < 0, before any multiplication. This is a pure argument-validation panic; count == 0 returns an empty slice and is explicitly allowed, and the subsequent multiplication overflow is a separate panic (error 1427).
Source
Thrown at src/bytes/bytes.go:639
//
// Note that this comment is not part of the doc comment.
//
//go:linkname Repeat
// Repeat returns a new byte slice consisting of count copies of b.
//
// It panics if count is negative or if the result of (len(b) * count)
// overflows.
func Repeat(b []byte, count int) []byte {
if count == 0 {
return []byte{}
}
// Since we cannot return an error on overflow,
// we should panic if the repeat will generate an overflow.
// See golang.org/issue/16237.
if count < 0 {
panic("bytes: negative Repeat count")
}
hi, lo := bits.Mul(uint(len(b)), uint(count))
if hi > 0 || lo > uint(maxInt) {
panic("bytes: Repeat output length overflow")
}
n := int(lo) // lo = len(b) * count
if len(b) == 0 {
return []byte{}
}
// Past a certain chunk size it is counterproductive to use
// larger chunks as the source of the write, as when the source
// is too large we are basically just thrashing the CPU D-cache.
// So if the result length is larger than an empirically-found
// limit (8KB), we stop growing the source string once the limit
// is reached and keep reusing the same source string - that
// should therefore be always resident in the L1 cache - until weView on GitHub (pinned to b6b368adc5)
Solutions
- Validate count >= 0 before calling Repeat (decide whether 0 is acceptable for your use case).
- If count is computed as a difference, clamp the floor at 0.
- Range-check any externally sourced multiplier against both 0 and a sane upper bound.
Example fix
// before
pad := bytes.Repeat([]byte(" "), width - cur) // negative when width < cur
// after
n := width - cur
if n < 0 {
n = 0
}
pad := bytes.Repeat([]byte(" "), n) Defensive patterns
Strategy: validation
Validate before calling
// Repeat only with a non-negative count.
func safeRepeat(b []byte, count int) ([]byte, error) {
if count < 0 {
return nil, fmt.Errorf("repeat count %d must be >= 0", count)
}
return bytes.Repeat(b, count), nil
} Prevention
- Range-check externally sourced multipliers before Repeat.
- Clamp computed counts (e.g. width - cur) to a floor of 0.
- Remember count == 0 is valid and returns an empty slice; only negative is rejected.
When it happens
Trigger: Calling bytes.Repeat(b, count) with count < 0; computing count as a subtraction (a - b) that goes negative; forwarding an unvalidated signed length from a parsed field; off-by-one where count = desiredLen/len(b) - 1 underflows when len(b) > desiredLen.
Common situations: Generating padding/indentation strings where the padding width is derived from a column that can be negative; repeating a pattern based on a user-supplied multiplier that is not range-checked; computing repetitions from a target length and a divisor, with the divisor larger than the target.
Related errors
- bytes.Buffer: truncation out of range
- bytes.Buffer.Grow: negative count
- bytes: Repeat output length overflow
- bytes.Buffer: too large
- bytes.Buffer: UnreadRune: previous operation was not a succe
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/7cc0d04923ffeb88.
Report an issue: GitHub.