{"record":{"id":"7e1f26b5f1a33d2a","repo":"go-sql-driver/mysql","slug":"illegal-s-packet-length-d","errorCode":null,"errorMessage":"illegal %s packet length %d","messagePattern":"illegal (.+?) packet length (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"utils.go","lineNumber":413,"sourceCode":"\tvar p1, p2, p3 byte // current digit pair\n\n\tswitch length {\n\tcase 10, 19, 21, 22, 23, 24, 25, 26:\n\tdefault:\n\t\tt := \"DATE\"\n\t\tif length > 10 {\n\t\t\tt += \"TIME\"\n\t\t}\n\t\treturn nil, fmt.Errorf(\"illegal %s length %d\", t, length)\n\t}\n\tswitch len(src) {\n\tcase 4, 7, 11:\n\tdefault:\n\t\tt := \"DATE\"\n\t\tif length > 10 {\n\t\t\tt += \"TIME\"\n\t\t}\n\t\treturn nil, fmt.Errorf(\"illegal %s packet length %d\", t, len(src))\n\t}\n\tdst = make([]byte, 0, length)\n\t// start with the date\n\tyear := binary.LittleEndian.Uint16(src[:2])\n\tpt := year / 100\n\tp1 = byte(year - 100*uint16(pt))\n\tp2, p3 = src[2], src[3]\n\tdst = append(dst,\n\t\tdigits10[pt], digits01[pt],\n\t\tdigits10[p1], digits01[p1], '-',\n\t\tdigits10[p2], digits01[p2], '-',\n\t\tdigits10[p3], digits01[p3],\n\t)\n\tif length == 10 {\n\t\treturn dst, nil\n\t}\n\tif len(src) == 4 {\n\t\treturn append(dst, zeroDateTime[10:length]...), nil","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/03d76c7e07908e255ce62d126d07ede3f2365d86/utils.go#L395-L431","documentation":"Raised by formatBinaryDateTime (utils.go:413) for the actual PAYLOAD length. When formatting a binary DATE/DATETIME to text, the driver requires the received byte slice length to be exactly 4, 7, or 11 (DATE, DATETIME, DATETIME-with-fractional). Any other length indicates the server sent a malformed binary value or the read pointer is desynchronized.","triggerScenarios":"Reading DATE/DATETIME/TIMESTAMP via a prepared statement (parseTime=false) when the server returns a binary temporal value whose length is not 4/7/11. Reached via formatBinaryDateTime at packets.go:1434 with src = data[pos:pos+num]. Indicates packet desync, truncation, or a non-conforming MySQL-compatible server.","commonSituations":"Result-set desynchronization after an earlier packet parse error, a proxy that re-encodes temporal values incorrectly, network corruption, or an exotic server fork with non-standard binary DATETIME encoding.","solutions":["Check server/proxy compatibility and upgrade; capture the failing packet for analysis.","Read the column as raw bytes ([]byte) or CAST to CHAR in SQL to avoid binary decoding.","Switch the query to the text protocol (non-prepared) for the affected column.","Restart the connection pool to clear any desync state."],"exampleFix":"-- force server-side text conversion\nSELECT CAST(created_at AS CHAR) AS created_at FROM t WHERE id = ?;\n\n// Go: scan into []byte / string\nvar s string\ndb.QueryRow(query, id).Scan(&s)","handlingStrategy":"try-catch","validationCode":"// Payload length is server-controlled; no client-side pre-check exists.\n// Mitigate by validating the server speaks the standard binary protocol.\nfunc isStandardBinaryDatePacketLen(n int) bool {\n    switch n {\n    case 4, 7, 11:\n        return true\n    }\n    return false\n}","typeGuard":"func isStandardBinaryDatePacketLen(n int) bool {\n    switch n {\n    case 4, 7, 11:\n        return true\n    }\n    return false\n}","tryCatchPattern":"var s string\nif err := db.QueryRow(q, id).Scan(&s); err != nil {\n    if strings.Contains(err.Error(), \"packet length\") {\n        var raw []byte\n        _ = db.QueryRow(`SELECT CAST(col AS CHAR) FROM t WHERE id=?`, id).Scan(&raw)\n        s = string(raw)\n    }\n}","preventionTips":["Restart the connection pool after packet errors to clear desync.","Prefer text-protocol (CAST AS CHAR) for DATETIME through non-conforming servers.","Upgrade/patch proxies that mishandle binary temporal values."],"tags":["mysql","datetime","binary-protocol","prepared-statements","protocol-error","packet-desync"],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}