{"id":"1b8230956a1bdafa","repo":"go-sql-driver/mysql","slug":"invalid-datetime-packet-length-d","errorCode":null,"errorMessage":"invalid DATETIME packet length %d","messagePattern":"invalid DATETIME packet length (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"utils.go","lineNumber":265,"sourceCode":"\t\t\tint(data[4]),                              // hour\n\t\t\tint(data[5]),                              // minutes\n\t\t\tint(data[6]),                              // seconds\n\t\t\t0,\n\t\t\tloc,\n\t\t), nil\n\tcase 11:\n\t\treturn time.Date(\n\t\t\tint(binary.LittleEndian.Uint16(data[:2])), // year\n\t\t\ttime.Month(data[2]),                       // month\n\t\t\tint(data[3]),                              // day\n\t\t\tint(data[4]),                              // hour\n\t\t\tint(data[5]),                              // minutes\n\t\t\tint(data[6]),                              // seconds\n\t\t\tint(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds\n\t\t\tloc,\n\t\t), nil\n\t}\n\treturn nil, fmt.Errorf(\"invalid DATETIME packet length %d\", num)\n}\n\nfunc appendDateTime(buf []byte, t time.Time, timeTruncate time.Duration) ([]byte, error) {\n\tif timeTruncate > 0 {\n\t\tt = t.Truncate(timeTruncate)\n\t}\n\n\tyear, month, day := t.Date()\n\thour, min, sec := t.Clock()\n\tnsec := t.Nanosecond()\n\n\tif year < 1 || year > 9999 {\n\t\treturn buf, errors.New(\"year is not in the range [1, 9999]: \" + strconv.Itoa(year)) // use errors.New instead of fmt.Errorf to avoid year escape to heap\n\t}\n\tyear100 := year / 100\n\tyear1 := year % 100\n\n\tvar localBuf [len(\"2006-01-02T15:04:05.999999999\")]byte // does not escape","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/utils.go#L247-L283","documentation":"Thrown by parseBinaryDateTime (utils.go:265) — the fallback of its switch — when decoding a BINARY-protocol DATETIME value whose declared length `num` is not one of {0, 4, 7, 11}. MySQL's binary protocol only sends DATETIME payloads of those exact lengths (date / date+time / date+time+fraction), so any other length is a protocol violation.","triggerScenarios":"Reading a DATETIME/TIMESTAMP column over the binary (prepared-statement) protocol where the server advertises an unsupported payload length — e.g. a corrupted or truncated binary row packet, or a non-conforming server/proxy. Fires during Scan on rows from a prepared statement (db.Query with ? placeholders uses the binary protocol).","commonSituations":"Packet corruption from a flaky network or a misbehaving proxy/router between app and MySQL; an exotic MySQL fork that emits non-standard binary datetime lengths; an old/new server version mismatch; memory/packet buffer corruption.","solutions":["Run the same prepared-statement query in the mysql CLI (or with a plain db.Query without ? to force the text protocol) and see if it succeeds.","Inspect the network path for proxies/routers that may mangle binary row data; connect directly to MySQL.","Confirm server version and that the column is a real DATETIME/TIMESTAMP.","If reproducible only via a specific proxy, file a bug with the proxy vendor; meanwhile disable compression and proxies."],"exampleFix":"// before: binary protocol via prepared statement\nrows, err := db.Query(\"SELECT created_at FROM orders WHERE id = ?\", id)\n\n// after: force text protocol to bypass the bad binary path for diagnosis\nrows, err := db.Query(\"SELECT created_at FROM orders WHERE id = \" + strconv.Itoa(id))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"var t time.Time\nif err := rows.Scan(&t); err != nil {\n    if strings.Contains(err.Error(), \"invalid DATETIME packet length\") {\n        // binary-protocol corruption; retry once, or fall back to text protocol\n    }\n}","preventionTips":["Use the text protocol (plain db.Query without ?) for environments with flaky binary-row paths.","Eliminate proxies/routers that mangle binary datetime payloads.","Keep server and driver versions on supported, compatible releases."],"tags":["datetime","binary-protocol","prepared-statement","wire-format"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}