AlexxIT/go2rtc · error

hap: read buffer is too small

Error message

hap: read buffer is too small

What it means

HAP encrypted-connection Read guard: the caller's buffer capacity is below packetSizeMax (0x400), the maximum encrypted HAP packet length. Because Read must be able to hold a full ciphertext block plus overhead, an undersized buffer is rejected up front rather than truncating.

Solutions

  1. Allocate the read buffer with at least 1024 bytes (make([]byte, 1024) or bufio with sufficient size)
  2. Use ReadTo/WriteTo-style helpers if the caller cannot size buffers
  3. Audit custom io.Reader consumers of hap.Conn for small buffer pools
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/hap/conn.go:85 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/4de360392fc1c6e1. Report an issue: GitHub.

Appendix: source

Thrown at pkg/hap/conn.go:85

	} else {
		c.encryptKey, c.decryptKey = key1, key2
	}

	return c, nil
}

const (
	// packetSizeMax is the max length of encrypted packets
	packetSizeMax = 0x400

	VerifySize = 2
	NonceSize  = 8
	Overhead   = 16 // chacha20poly1305.Overhead
)

func (c *Conn) Read(b []byte) (n int, err error) {
	if cap(b) < packetSizeMax {
		return 0, errors.New("hap: read buffer is too small")
	}

	verify := make([]byte, VerifySize) // verify = plain message size
	if _, err = io.ReadFull(c.rw, verify); err != nil {
		return
	}

	n = int(binary.LittleEndian.Uint16(verify))

	ciphertext := make([]byte, n+Overhead)
	if _, err = io.ReadFull(c.rw, ciphertext); err != nil {
		return
	}

	nonce := make([]byte, NonceSize)
	binary.LittleEndian.PutUint64(nonce, c.decryptCnt)
	c.decryptCnt++

View on GitHub (pinned to c245815e75)