{"record":{"id":"1f5c249e2decaada","repo":"EllanJiang/GameFramework","slug":"target-length-is-invalid","errorCode":null,"errorMessage":"Target length is invalid.","messagePattern":"Target length is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Network/NetworkManager.ReceiveState.cs","lineNumber":89,"sourceCode":"                }\n\n                if (disposing)\n                {\n                    if (m_Stream != null)\n                    {\n                        m_Stream.Dispose();\n                        m_Stream = null;\n                    }\n                }\n\n                m_Disposed = true;\n            }\n\n            private void Reset(int targetLength, IPacketHeader packetHeader)\n            {\n                if (targetLength < 0)\n                {\n                    throw new GameFrameworkException(\"Target length is invalid.\");\n                }\n\n                m_Stream.Position = 0L;\n                m_Stream.SetLength(targetLength);\n                m_PacketHeader = packetHeader;\n            }\n        }\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":99,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Network/NetworkManager.ReceiveState.cs#L71-L99","documentation":"ReceiveState.Reset sizes the internal receive stream to the incoming packet's length; a negative targetLength is invalid and throws GameFrameworkException. Called from PrepareForPacketHeader and PrepareForPacket, so a header reporting a negative packet length triggers it.","triggerScenarios":"A deserialized IPacketHeader whose PacketLength is negative (corrupted header data, wrong endianness when reading the length field, signed overflow); custom headers computing length incorrectly.","commonSituations":"Header length field parsed with wrong byte order producing huge/negative values; protocol mismatch where a payload byte is interpreted as part of the length; int overflow when the length field is unsigned in the protocol but signed in C#.","solutions":["Fix the header deserialization so PacketLength is read with the correct endianness and width used by the server.","Validate PacketLength >= 0 (and <= a sane maximum) in your DeserializePacketHeader and fail the header step there.","Check for signed/unsigned conversion bugs on the length field (use uint/long parsing where the protocol is unsigned)."],"exampleFix":"// before\nint length = reader.ReadInt16(); // wrong width/endianness\n// after\nint length = (int)BinaryPrimitives.ReadUInt32BigEndian(buffer); // matches server header layout","handlingStrategy":"validation","validationCode":"public static bool IsValidHeader(IPacketHeader h) => h != null && h.PacketLength >= 0 && h.PacketLength <= MaxPacketSize;","typeGuard":"public static bool HasValidLength(IPacketHeader h) => h is { PacketLength: >= 0 };","tryCatchPattern":"try { PrepareReceive(header); } catch (GameFrameworkException ex) when (ex.Message == \"Target length is invalid.\") { CloseAndReconnect(channel); }","preventionTips":["Read the length field with the protocol's exact width and endianness","Clamp/validate PacketLength against a sane maximum before use","Use unsigned parsing for unsigned protocol fields","Fuzz-test header deserialization against corrupted buffers"],"tags":["network","protocol-mismatch","value-out-of-range","gameframework"],"backgroundTag":"value-out-of-range","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}