{"record":{"id":"dac86316a37709da","repo":"XTLS/Xray-core","slug":"read-bytes-invalid-size-d","errorCode":null,"errorMessage":"read bytes: invalid size: %d","messagePattern":"read bytes: invalid size: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":320,"sourceCode":"func (v *UUID) writeTo(w io.Writer) error {\n\t_, err := w.Write(v[:])\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write UUID: %w\", err)\n\t}\n\treturn nil\n}\n\ntype Bytes []byte\n\nfunc (v *Bytes) readFrom(r io.Reader) error {\n\tvar length Varint\n\terr := length.readFrom(r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read bytes: %w\", err)\n\t}\n\n\tif length < 0 || length >= 1024 {\n\t\treturn fmt.Errorf(\"read bytes: invalid size: %d\", length)\n\t}\n\n\tbuf := make([]byte, length)\n\n\t_, err = io.ReadFull(r, buf)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read bytes: %w\", err)\n\t}\n\n\t*v = append([]byte(*v), buf...)\n\n\treturn nil\n}\n\ntype RestBytes []byte\n\nfunc (v *RestBytes) readFrom(r io.Reader) error {\n\tbuf, err := io.ReadAll(r)","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L302-L338","documentation":"After reading the Varint length of a Bytes field, the reader requires 0 <= length < 1024. This error fires when the decoded length is negative or >= 1024, which cannot be a legitimate field size in this protocol and almost always means the stream is corrupted or desynchronized — the reader is interpreting payload bytes as a length prefix.","triggerScenarios":"Any prior read consuming the wrong number of bytes (mismatched field order, wrong packet ID dispatch, padding schedule mismatch) so this reader starts mid-payload; or a malicious/garbage endpoint sending an out-of-range varint.","commonSituations":"Client and server built from different protocol versions (field order changed); pointing the transport at a plain TCP service that sends arbitrary bytes; a proxy mangling the byte stream; fuzzing input reaching the decoder.","solutions":["Verify both endpoints use the same protocol version and padding preset.","Confirm the address actually serves this Minecraft-based protocol (not a status/legacy endpoint).","Audit recent changes to packet struct field order or optional-field conditions.","Treat persistent occurrence as a hard failure: the stream cannot be resynchronized; drop the connection."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if err := bytesField.readFrom(r); err != nil { /* ... */ }\n// pre-empt: only route connections you know speak this protocol;\n// verify first bytes look like a valid handshake before enabling the codec","typeGuard":null,"tryCatchPattern":"if err := bytesField.readFrom(r); err != nil {\n    if strings.Contains(err.Error(), \"invalid size\") {\n        return fmt.Errorf(\"stream desync or wrong endpoint: %w\", err)\n    }\n    return err\n}","preventionTips":["Pin identical protocol/preset versions on client and server.","Never point the transport at arbitrary TCP services.","On this error, drop the connection immediately — the stream cannot be resynchronized."],"tags":["go","protocol-framing","corruption","validation"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}