{"record":{"id":"4de360392fc1c6e1","repo":"AlexxIT/go2rtc","slug":"hap-read-buffer-is-too-small","errorCode":null,"errorMessage":"hap: read buffer is too small","messagePattern":"hap: read buffer is too small","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hap/conn.go","lineNumber":85,"sourceCode":"\t} else {\n\t\tc.encryptKey, c.decryptKey = key1, key2\n\t}\n\n\treturn c, nil\n}\n\nconst (\n\t// packetSizeMax is the max length of encrypted packets\n\tpacketSizeMax = 0x400\n\n\tVerifySize = 2\n\tNonceSize  = 8\n\tOverhead   = 16 // chacha20poly1305.Overhead\n)\n\nfunc (c *Conn) Read(b []byte) (n int, err error) {\n\tif cap(b) < packetSizeMax {\n\t\treturn 0, errors.New(\"hap: read buffer is too small\")\n\t}\n\n\tverify := make([]byte, VerifySize) // verify = plain message size\n\tif _, err = io.ReadFull(c.rw, verify); err != nil {\n\t\treturn\n\t}\n\n\tn = int(binary.LittleEndian.Uint16(verify))\n\n\tciphertext := make([]byte, n+Overhead)\n\tif _, err = io.ReadFull(c.rw, ciphertext); err != nil {\n\t\treturn\n\t}\n\n\tnonce := make([]byte, NonceSize)\n\tbinary.LittleEndian.PutUint64(nonce, c.decryptCnt)\n\tc.decryptCnt++\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/hap/conn.go#L67-L103","documentation":"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.","triggerScenarios":"Thrown at pkg/hap/conn.go:85 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Allocate the read buffer with at least 1024 bytes (make([]byte, 1024) or bufio with sufficient size)","Use ReadTo/WriteTo-style helpers if the caller cannot size buffers","Audit custom io.Reader consumers of hap.Conn for small buffer pools"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}