golang/go · error

bytes: Join output length overflow

Error message

bytes: Join output length overflow

What it means

bytes.Join concatenates the elements of s with sep between them. Before allocating it computes the total length; it panics with "bytes: Join output length overflow" at the separator step when len(sep) >= maxInt/(len(s)-1), i.e. when the total separator contribution alone (len(sep) * (len(s)-1)) would overflow. This guards the multiplication, not the final allocation.

Source

Thrown at src/bytes/bytes.go:564

	return a
}

// Join concatenates the elements of s to create a new byte slice. The separator
// sep is placed between elements in the resulting slice.
func Join(s [][]byte, sep []byte) []byte {
	if len(s) == 0 {
		return []byte{}
	}
	if len(s) == 1 {
		// Just return a copy.
		return append([]byte(nil), s[0]...)
	}

	var n int
	if len(sep) > 0 {
		if len(sep) >= maxInt/(len(s)-1) {
			panic("bytes: Join output length overflow")
		}
		n += len(sep) * (len(s) - 1)
	}
	for _, v := range s {
		if len(v) > maxInt-n {
			panic("bytes: Join output length overflow")
		}
		n += len(v)
	}

	b := bytealg.MakeNoZero(n)[:n:n]
	bp := copy(b, s[0])
	for _, v := range s[1:] {
		bp += copy(b[bp:], sep)
		bp += copy(b[bp:], v)
	}
	return b
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Reduce the element count: batch and write incrementally to an io.Writer instead of Join into one []byte.
  2. Validate that the expected total (len(s)-1)*len(sep) fits before calling Join.
  3. If a separator is only needed for output formatting, prefer a streaming writer (bufio.Writer) that does not pre-size a single contiguous slice.

Example fix

// before
out := bytes.Join(chunks, []byte("\n")) // len(chunks) huge -> overflow

// after
var w io.Writer = buf
for i, c := range chunks {
    if i > 0 {
        w.Write([]byte("\n"))
    }
    w.Write(c)
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check separator contribution before Join.
func safeJoinSep(s [][]byte, sep []byte) ([]byte, error) {
    if len(sep) > 0 && len(s) > 1 {
        if int64(len(sep)) >= int64(maxInt)/int64(len(s)-1) {
            return nil, fmt.Errorf("join separator overflow")
        }
    }
    return bytes.Join(s, sep), nil
}

Prevention

When it happens

Trigger: Joining a very large number of slices (huge len(s)) with a non-empty separator such that sep*(len(s)-1) overflows; building CSV/TSV/delimited output where element count is attacker-controlled; joining millions of small fragments with a multi-byte separator.

Common situations: Aggregating log lines or tokens into one buffer with a delimiter; serializing a large slice of fields with a separator; producing output where len(s) came from unbounded streaming collection.

Related errors


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