{"record":{"id":"431c6b0463386140","repo":"XTLS/Xray-core","slug":"write-packet-id-w","errorCode":null,"errorMessage":"write packet ID: %w","messagePattern":"write packet ID: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":420,"sourceCode":"\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 {\n\t\t\treturn io.ErrShortWrite\n\t\t}\n\t}","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L402-L438","documentation":"Returned by encodePacket when writing the packet ID varint into the in-memory frame bytes.Buffer fails. As with the frame-length write, bytes.Buffer writes are infallible for the shipped Varint type, making this a defensive error path satisfying the field.writeTo contract rather than a realistic failure.","triggerScenarios":"packetIDVarint.writeTo(&frame) returning an error at protocol.go:420 — unreachable with stock field types writing to bytes.Buffer.","commonSituations":"Not observed in production; only reachable via custom field implementations or a modified encoder.","solutions":["Inspect the wrapped error for the real cause if using custom field types","Verify the local tree has not modified Varint.writeTo","Report upstream if reproducible with stock types"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Recognize this as an unreachable defensive path with stock types","Audit custom field implementations if it ever appears"],"tags":["go","minecraft-protocol","serialization","defensive","xmc"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}