TooTallNate/Java-WebSocket · error · InvalidDataException
1002
1002
Error message
Negative count
What it means
checkAlloc validates a byte count used to size buffers/reads during frame parsing. A negative count would allocate a negative-size buffer, so Draft throws InvalidDataException with close code 1002 (protocol error), meaning the remote peer sent a malformed frame length that made the computed size negative.
Solutions
- Identify the peer producing malformed frames (log the remote address) and fix or block it
- Ensure intermediate proxies/devices do not corrupt TCP streams
- Upgrade java-websocket; newer versions handle malformed lengths more robustly
- The connection will be closed with code 1002 — treat this as a peer protocol violation and reconnect cleanly
Defensive patterns
Strategy: try-catch
Validate before calling
// server side: reject obviously malformed frames before they reach draft parsing if (payloadLength < 0 || payloadLength > (1L << 63) - 1) throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "bad length");
Try / catch
webSocket.setWebSocketFactory(...);
// at connection level:
onError(WebSocket conn, Exception ex) {
if (ex instanceof InvalidDataException && ((InvalidDataException) ex).getCloseCode() == 1002) {
logger.warn("Peer sent malformed frame (protocol error), closing: {}", conn.getRemoteSocketAddress());
}
} Prevention
- Treat 1002 closes as peer protocol violations, not your bug
- Log remote addresses of peers causing repeated 1002s and rate-limit/block them
- Keep java-websocket updated for hardened frame parsing
- Ensure intermediaries do not truncate or rewrite TCP payloads
When it happens
Trigger: A peer sends a frame whose decoded length field results in a negative byte count, typically a malformed or malicious frame; also reachable from caller code passing negative sizes into draft parsing paths.
Common situations: Connecting to a non-conformant or corrupted WebSocket peer, packet corruption, proxies truncating/altering frames, fuzzed clients hitting your server.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Only Opcode.BINARY or Opcode.TEXT are allowed
- Size representation not supported/specified
- 1002
- bad rsv RSV1: RSV2: RSV3
- 1002
AI-assisted analysis of TooTallNate/Java-WebSocket@afeacbf8c0 (2026-09-09).
Data as JSON: /api/errors/dc3d7522278203a2.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/org/java_websocket/drafts/Draft.java:324
public abstract CloseHandshakeType getCloseHandshakeType();
/**
* Drafts must only be by one websocket at all. To prevent drafts to be used more than once the
* Websocket implementation should call this method in order to create a new usable version of a
* given draft instance.<br> The copy can be safely used in conjunction with a new websocket
* connection.
*
* @return a copy of the draft
*/
public abstract Draft copyInstance();
public Handshakedata translateHandshake(ByteBuffer buf) throws InvalidHandshakeException {
return translateHandshakeHttp(buf, role);
}
public int checkAlloc(int bytecount) throws InvalidDataException {
if (bytecount < 0) {
throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Negative count");
}
return bytecount;
}
int readVersion(Handshakedata handshakedata) {
String vers = handshakedata.getFieldValue("Sec-WebSocket-Version");
if (vers.length() > 0) {
int v;
try {
v = Integer.parseInt(vers.trim());
return v;
} catch (NumberFormatException e) {
return -1;
}
}
return -1;
}
View on GitHub (pinned to afeacbf8c0)