{"record":{"id":"2a5eabec40d34e12","repo":"apache/cassandra","slug":"a-counterid-representation-is-exactly-length-by","errorCode":null,"errorMessage":"A CounterId representation is exactly ${LENGTH} bytes","messagePattern":"A CounterId representation is exactly (.+?) bytes","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/CounterId.java","lineNumber":82,"sourceCode":"     * For performance reasons, this function interns the provided ByteBuffer.\n     */\n    public static CounterId wrap(ByteBuffer id)\n    {\n        return new CounterId(id);\n    }\n\n    public static CounterId wrap(ByteBuffer bb, int offset)\n    {\n        ByteBuffer dup = bb.duplicate();\n        dup.position(offset);\n        dup.limit(dup.position() + LENGTH);\n        return wrap(dup);\n    }\n\n    private CounterId(ByteBuffer id)\n    {\n        if (id.remaining() != LENGTH)\n            throw new IllegalArgumentException(\"A CounterId representation is exactly \" + LENGTH + \" bytes\");\n\n        this.id = id;\n    }\n\n    public static CounterId generate()\n    {\n        return new CounterId(ByteBuffer.wrap(nextTimeUUIDAsBytes()));\n    }\n\n    /*\n     * For performance reasons, this function returns a reference to the internal ByteBuffer. Clients not modify the\n     * result of this function.\n     */\n    public ByteBuffer bytes()\n    {\n        return id;\n    }\n","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/CounterId.java#L64-L100","documentation":"A CounterId wraps exactly LENGTH (16) bytes in a ByteBuffer. The private constructor validates the buffer's remaining bytes and throws IllegalArgumentException if the representation has any other size.","triggerScenarios":"CounterId.wrap / private construction with a ByteBuffer whose remaining() != 16, e.g. a truncated or padded counter-id column name, or a wrong-length byte array converted to a buffer.","commonSituations":"Corrupted counter columns, parsing metadata of the wrong format, hand-crafting counter IDs from UUIDs/strings without confirming byte length.","solutions":["Ensure the ByteBuffer is backed by exactly 16 bytes (e.g. ByteBuffer.wrap(idBytes) with idBytes.length == 16)","Use CounterId.wrap(ByteBuffer) / CounterId.fromString / generate() instead of constructing raw buffers","Validate input length before calling: if (buf.remaining() != 16) handle the error"],"exampleFix":"// before\nCounterId id = CounterId.wrap(ByteBuffer.wrap(new byte[8]));\n// after\nbyte[] raw = new byte[16];\nCounterId id = CounterId.wrap(ByteBuffer.wrap(raw));","handlingStrategy":"validation","validationCode":"CounterId safeWrap(ByteBuffer buf) {\n    if (buf == null || buf.remaining() != 16)\n        throw new IllegalArgumentException(\"CounterId needs 16 bytes, got \" + (buf == null ? 0 : buf.remaining()));\n    return CounterId.wrap(buf);\n}","typeGuard":null,"tryCatchPattern":"try {\n    CounterId id = CounterId.wrap(buf);\n} catch (IllegalArgumentException e) {\n    // wrong-length representation: corrupt data or wrong format\n}","preventionTips":["Always construct counter IDs via generate()/fromString/wrap with 16-byte sources","Validate column-name length before interpreting as a CounterId","Never slice counter IDs from arbitrary-length byte arrays"],"tags":["counter-id","length-check","validation","java"],"backgroundTag":"invalid-argument-value","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}