{"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/c426bd93799de0f0e094c8f0582872c529d0ed0a/utils.go#L395-L431","documentation":"Thrown by formatBinaryDateTime (utils.go:413) when the ACTUAL payload byte length `len(src)` is not one of {4, 7, 11} — the only valid binary DATE/DATETIME payload sizes (date / date+time / date+time+micros). Distinct from error 47 which checks the declared column length; this checks the real bytes received.","triggerScenarios":"Reading a DATE/DATETIME column via the binary protocol where the actual bytes in the row packet don't match a valid payload size — truncated packets, padding corruption, or a non-conforming server. Fires during Scan on prepared-statement rows.","commonSituations":"Truncated/corrupted binary row packets over a flaky link; a proxy/router that mishandles binary datetime payloads; server-version mismatch; rare memory corruption. The message reports the actual (invalid) byte count.","solutions":["Note the actual length in the message and reproduce with the text protocol (CAST AS CHAR) to confirm whether the binary path is the culprit.","Eliminate proxies/tunnels; connect directly to MySQL and retry the prepared statement.","Verify server version and column type with SHOW COLUMNS.","If it reproduces against vanilla MySQL, report upstream with the length value and column DDL."],"exampleFix":"// before\nvar t time.Time\ndb.QueryRow(\"SELECT d FROM t WHERE id=?\", id).Scan(&t)\n\n// after\nvar raw string\nif err := db.QueryRow(\"SELECT CAST(d AS CHAR) FROM t WHERE id=?\", id).Scan(&raw); err == nil {\n    t, err = time.ParseInLocation(\"2006-01-02 15:04:05\", raw, time.Local)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := rows.Scan(...); err != nil {\n    if strings.Contains(err.Error(), \"illegal \") && strings.Contains(err.Error(), \"packet length\") {\n        // actual binary payload size is wrong; fall back to text protocol\n    }\n}","preventionTips":["For corrupted binary-row environments, force the text protocol via CAST AS CHAR or plain Query.","Remove proxies/routers that truncate binary datetime payloads.","Monitor network health to catch truncation early."],"tags":["datetime","binary-protocol","wire-format","prepared-statement"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}