EllanJiang/GameFramework · error · GameFrameworkException
Target length is invalid.
Error message
Target length is invalid.
What it means
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.
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).
Example fix
// before int length = reader.ReadInt16(); // wrong width/endianness // after int length = (int)BinaryPrimitives.ReadUInt32BigEndian(buffer); // matches server header layout
Defensive patterns
Strategy: validation
Validate before calling
public static bool IsValidHeader(IPacketHeader h) => h != null && h.PacketLength >= 0 && h.PacketLength <= MaxPacketSize;
Type guard
public static bool HasValidLength(IPacketHeader h) => h is { PacketLength: >= 0 }; Try / catch
try { PrepareReceive(header); } catch (GameFrameworkException ex) when (ex.Message == "Target length is invalid.") { CloseAndReconnect(channel); } Prevention
- 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
When it happens
Trigger: A deserialized IPacketHeader whose PacketLength is negative (corrupted header data, wrong endianness when reading the length field, signed overflow); custom headers computing length incorrectly.
Common situations: 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#.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Packet header is invalid.
- You must connect first.
- Socket is not active.
- Packet is invalid.
- Serialized packet failure.
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/1f5c249e2decaada.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Network/NetworkManager.ReceiveState.cs:89
}
if (disposing)
{
if (m_Stream != null)
{
m_Stream.Dispose();
m_Stream = null;
}
}
m_Disposed = true;
}
private void Reset(int targetLength, IPacketHeader packetHeader)
{
if (targetLength < 0)
{
throw new GameFrameworkException("Target length is invalid.");
}
m_Stream.Position = 0L;
m_Stream.SetLength(targetLength);
m_PacketHeader = packetHeader;
}
}
}
}
View on GitHub (pinned to d0c010b051)