golang/go · error

bytes.Buffer.Grow: negative count

Error message

bytes.Buffer.Grow: negative count

What it means

Buffer.Grow(n) reserves space so that at least n more bytes can be written without reallocation. It panics with "bytes.Buffer.Grow: negative count" when n < 0, before any growth arithmetic. This is a pure input-validation panic; a successful Grow still may later panic with ErrTooLarge if the buffer cannot grow (delegated to grow).

Source

Thrown at src/bytes/buffer.go:184

		panic(ErrTooLarge)
	} else {
		// Add b.off to account for b.buf[:b.off] being sliced off the front.
		b.buf = growSlice(b.buf[b.off:], b.off+n)
	}
	// Restore b.off and len(b.buf).
	b.off = 0
	b.buf = b.buf[:m+n]
	return m
}

// Grow grows the buffer's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to the
// buffer without another allocation.
// If n is negative, Grow will panic.
// If the buffer can't grow it will panic with [ErrTooLarge].
func (b *Buffer) Grow(n int) {
	if n < 0 {
		panic("bytes.Buffer.Grow: negative count")
	}
	m := b.grow(n)
	b.buf = b.buf[:m]
}

// Write appends the contents of p to the buffer, growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large, Write will panic with [ErrTooLarge].
func (b *Buffer) Write(p []byte) (n int, err error) {
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(len(p))
	if !ok {
		m = b.grow(len(p))
	}
	return copy(b.buf[m:], p), nil
}

// WriteString appends the contents of s to the buffer, growing the buffer as

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Validate n >= 0 before calling Grow.
  2. If computing n as a difference, clamp to a floor of 0 (and decide whether 0 is meaningful; Grow(0) is a no-op-equivalent but allowed).
  3. When pre-sizing from external input, range-check against both 0 and a sane upper bound before Grow.

Example fix

// before
want := targetCap - b.Len() // can be negative
b.Grow(want)

// after
want := targetCap - b.Len()
if want < 0 {
    want = 0
}
b.Grow(want)
Defensive patterns

Strategy: validation

Validate before calling

// Grow only with a non-negative size.
func safeGrow(b *bytes.Buffer, n int) error {
    if n < 0 {
        return fmt.Errorf("grow count %d must be >= 0", n)
    }
    b.Grow(n)
    return nil
}

Prevention

When it happens

Trigger: Calling b.Grow(n) with a negative n; computing n as a difference (a - b) that underflows when b > a; forwarding a size from a parsed field that was not range-checked; off-by-one where n = desired - current with current > desired.

Common situations: Pre-sizing a Buffer from user-controlled length fields; a Grow(capacity - b.Len()) where capacity < b.Len() after a reset/reuse miscalculation; size computed from a header that can legitimately be smaller than the current buffer.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/05329e6d6508903d. Report an issue: GitHub.