{"id":"64afcd66f29d4b21","repo":"go-sql-driver/mysql","slug":"invalid-compressed-packet-uncompressed-length-in","errorCode":null,"errorMessage":"invalid compressed packet: uncompressed length in header is %d, actual %d","messagePattern":"invalid compressed packet: uncompressed length in header is (.+?), actual (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compress.go","lineNumber":141,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\t// if payload is uncompressed, its length will be specified as zero, and its\n\t// true length is contained in comprLength\n\tif uncompressedLength == 0 {\n\t\tc.buff.Write(comprData)\n\t\treturn nil\n\t}\n\n\t// use existing capacity in bytesBuf if possible\n\tc.buff.Grow(uncompressedLength)\n\tnread, err := zDecompress(comprData, &c.buff)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif nread != uncompressedLength {\n\t\treturn fmt.Errorf(\"invalid compressed packet: uncompressed length in header is %d, actual %d\",\n\t\t\tuncompressedLength, nread)\n\t}\n\treturn nil\n}\n\nconst minCompressLength = 150\nconst maxPayloadLen = maxPacketSize - 4\n\n// writePackets sends one or some packets with compression.\n// Use this instead of mc.netConn.Write() when mc.compress is true.\nfunc (c *compIO) writePackets(packets []byte) (int, error) {\n\ttotalBytes := len(packets)\n\tblankHeader := make([]byte, 7)\n\tbuf := &c.buff\n\n\tfor len(packets) > 0 {\n\t\tpayloadLen := min(maxPayloadLen, len(packets))\n\t\tpayload := packets[:payloadLen]","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/compress.go#L123-L159","documentation":"Thrown by compIO.readCompressedPacket (compress.go:141) after zlib-decompressing a compressed packet whose resulting byte count does not match the uncompressedLength the server advertised in the 7-byte compressed header. A mismatch means the compressed stream was corrupted, truncated, or tampered with — the integrity contract of compressed mode is broken.","triggerScenarios":"Using the driver with compression enabled (DSN parameter compress=true) over a connection where a compressed packet's declared uncompressed length disagrees with the actually decompressed size — flaky network corrupting bytes, a proxy mangling compressed frames, or a server bug in compressed-mode framing.","commonSituations":"compress=true across a lossy/intercepted link or through a proxy that doesn't transparently pass compressed frames; MTU/fragmentation issues truncating compressed packets; server-side compression bugs in older MySQL versions; memory pressure corrupting buffers.","solutions":["Disable compression (remove compress=true from the DSN) — for most workloads on a LAN it adds CPU cost without net benefit, and removes this entire class of error.","If you need compression, rule out proxies/routers that mishandle compressed frames by connecting directly to MySQL.","Check network health (packet loss, MTU mismatches, NIC errors) on the link.","Upgrade the MySQL server in case of a known compression-framing bug."],"exampleFix":"// before\ndsn := \"user:pass@tcp(host:3306)/db?compress=true\"\n\n// after: drop compression unless you have measured a benefit\ndsn := \"user:pass@tcp(host:3306)/db\"","handlingStrategy":"validation","validationCode":"// only enable compression when you have measured a benefit and the link is clean\nfunc dsnWithCompression(base string, compress bool) string {\n    if !compress {\n        return strings.ReplaceAll(base, \"compress=true\", \"\")\n    }\n    return base\n}","typeGuard":null,"tryCatchPattern":"if err := rows.Err(); err != nil {\n    if strings.Contains(err.Error(), \"invalid compressed packet\") {\n        // disable compression in the DSN and reconnect\n    }\n}","preventionTips":["Default to no compression; most LAN workloads are faster without it.","If you compress, monitor packet loss and avoid proxies that mishandle compressed frames.","Keep server MySQL version current to benefit from compression-framing fixes."],"tags":["compression","wire-format","zlib","network"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}