{"record":{"id":"f589cd8a60c1bf5f","repo":"XTLS/Xray-core","slug":"write-packet-length-w","errorCode":null,"errorMessage":"write packet length: %w","messagePattern":"write packet length: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":417,"sourceCode":"\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"write packet field: %w\", err)\n\t\t}\n\t}\n\tif dataBuf.Len() > maxPacketDataLength {\n\t\treturn nil, fmt.Errorf(\"write packet: bad length: %d\", dataBuf.Len())\n\t}\n\n\tpacketIDVarint := Varint(packetID)\n\tbodyLength := varintSize(packetIDVarint) + dataBuf.Len()\n\tif bodyLength > maxPacketBodyLength {\n\t\treturn nil, fmt.Errorf(\"write packet: bad length: %d\", bodyLength)\n\t}\n\n\tvar frame bytes.Buffer\n\tframe.Grow(varintSize(Varint(bodyLength)) + bodyLength)\n\tframeLength := Varint(bodyLength)\n\tif err := frameLength.writeTo(&frame); err != nil {\n\t\treturn nil, fmt.Errorf(\"write packet length: %w\", err)\n\t}\n\tif err := packetIDVarint.writeTo(&frame); err != nil {\n\t\treturn nil, fmt.Errorf(\"write packet ID: %w\", err)\n\t}\n\tframe.Write(dataBuf.Bytes())\n\treturn frame.Bytes(), nil\n}\n\nfunc writeFull(w io.Writer, p []byte) error {\n\tfor len(p) > 0 {\n\t\tn, err := w.Write(p)\n\t\tif n > 0 {\n\t\t\tp = p[n:]\n\t\t}\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif n == 0 {","sourceCodeStart":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L399-L435","documentation":"Returned by encodePacket when writing the frame-length varint into an in-memory bytes.Buffer fails. bytes.Buffer.Write cannot fail (it grows as needed and only panics on overflow at ~MaxInt), so with the Varint field types shipped in this package this error is unreachable. It exists to satisfy the field.writeTo error contract.","triggerScenarios":"frameLength.writeTo(&frame) returning an error at protocol.go:417 — no stock field type can produce this against bytes.Buffer.","commonSituations":"Not seen in production; would require a custom Varint writeTo implementation or memory exhaustion (OOM panic, not this error).","solutions":["If hit, inspect the wrapped error — it must come from a custom field implementation","Confirm no local fork replaced Varint.writeTo with a fallible version","Report upstream as a suspected invariant violation"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Recognize this as an unreachable defensive path with stock types","Report upstream if reproducible — it indicates a fork or custom field bug"],"tags":["go","minecraft-protocol","serialization","defensive","xmc"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}