apache/cassandra · error · org.apache.cassandra.transport.ProtocolException
Invalid ByteBuf length " + length
Error message
Invalid ByteBuf length " + length
What it means
CBUtil.readValue(ByteBuf) treats a 4-byte length of -1 as null and -2 as the UNSET sentinel; any other negative length is invalid and throws ProtocolException "Invalid ByteBuf length <n>". Positive lengths are read normally. This rejects lengths outside the protocol's defined [-2, MAX] domain.
Solutions
- Fix the sender so value lengths are only >=0, -1 (null), or -2 (unset)
- Reconnect the session to resync the stream
- Verify compression negotiation (lz4/snappy) matches on both ends
- Capture frames to locate where the byte stream diverges
Example fix
// before buf.writeInt(-3); // invalid sentinel // after buf.writeInt(-1); // null, or -2 for UNSET, or >=0 payload length
Defensive patterns
Strategy: validation
Validate before calling
boolean isValidValueLength(int len) { return len >= 0 || len == -1 || len == -2; } Type guard
boolean isValidSentinel(int length) { return length == -1 || length == -2; } Try / catch
try { ... } catch (ProtocolException e) { if (e.getMessage().startsWith("Invalid ByteBuf length")) { reconnectSession(); } else throw e; } Prevention
- Only emit lengths of -1, -2, or >=0 in custom frames
- Resync/reconnect after any framing error
- Verify compression negotiation to avoid parsing garbage lengths
When it happens
Trigger: A serialized value field carries a length < -2 (e.g., corrupt int, bit-flipped frame, or misaligned read landing on non-length bytes).
Common situations: Stream desync after an earlier malformed message; memory corruption or buggy client writing garbage lengths; parsing compressed vs uncompressed frames inconsistently.
Related errors
- Cannot read value of length
- Not enough bytes to read a byte array preceded by its 2…
- Not enough bytes to read an UTF8 serialized string preceded…
- Not enough bytes to read an UTF8 serialized string preceded…
- Beta version of server used
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/5b6d224a4674e07c.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/transport/CBUtil.java:496
ByteBuffer buffer = cb.nioBuffer(cb.readerIndex(), length);
cb.skipBytes(length);
return buffer;
}
public static ByteBuffer readBoundValue(ByteBuf cb, ProtocolVersion protocolVersion)
{
int length = cb.readInt();
if (length < 0)
{
if (protocolVersion.isSmallerThan(ProtocolVersion.V4)) // backward compatibility for pre-version 4
return null;
if (length == -1)
return null;
else if (length == -2)
return ByteBufferUtil.UNSET_BYTE_BUFFER;
else
throw new ProtocolException("Invalid ByteBuf length " + length);
}
return ByteBuffer.wrap(readRawBytes(cb, length));
}
public static void writeValue(byte[] bytes, ByteBuf cb)
{
if (bytes == null)
{
cb.writeInt(-1);
return;
}
cb.writeInt(bytes.length);
cb.writeBytes(bytes);
}
public static void writeValue(ByteBuffer bytes, ByteBuf cb)
{View on GitHub (pinned to 88fd0f6a0e)