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

  1. Identify the peer producing malformed frames (log the remote address) and fix or block it
  2. Ensure intermediate proxies/devices do not corrupt TCP streams
  3. Upgrade java-websocket; newer versions handle malformed lengths more robustly
  4. 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

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


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)