{"record":{"id":"c96f04d0b9231d2a","repo":"apache/cassandra","slug":"expected-8-or-0-byte-long-d","errorCode":null,"errorMessage":"Expected 8 or 0 byte long (%d)","messagePattern":"Expected 8 or 0 byte long \\((.+?)\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/LongSerializer.java","lineNumber":43,"sourceCode":"\npublic class LongSerializer extends TypeSerializer<Long>\n{\n    public static final LongSerializer instance = new LongSerializer();\n\n    public <V> Long deserialize(V value, ValueAccessor<V> accessor)\n    {\n        return accessor.isEmpty(value) ? null : accessor.toLong(value);\n    }\n\n    public ByteBuffer serialize(Long value)\n    {\n        return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) != 8 && !accessor.isEmpty(value))\n            throw new MarshalException(String.format(\"Expected 8 or 0 byte long (%d)\", accessor.size(value)));\n    }\n\n    public String toString(Long value)\n    {\n        return value == null ? \"\" : String.valueOf(value);\n    }\n\n    public Class<Long> getType()\n    {\n        return Long.class;\n    }\n}\n","sourceCodeStart":25,"sourceCodeEnd":56,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/LongSerializer.java#L25-L56","documentation":"LongSerializer.validate enforces that a serialized long is either exactly 8 bytes or empty (null). Any other size is rejected with this MarshalException naming the offending byte count. LongType columns (and anything backed by long encoding) must carry a full 8-byte value.","triggerScenarios":"Binding a value of 0, 1-7, or >8 bytes to a bigint/LongType column — e.g. inserting a blob of the wrong length via CQL, or programmatic writes using the wrong serializer (int vs long).","commonSituations":"Inserting blob literals into bigint columns, client code writing ints (4 bytes) where longs are expected, migration scripts loading wrong-width data, hand-built buffers in tests.","solutions":["Ensure the value is exactly 8 bytes: use the serializer's serialize(Long) instead of hand-building the buffer.","Pass null/empty buffer if you intend a null value, not a zero-length-padded buffer.","Check the column type with DESCRIBE — you may be writing to a bigint column with int-sized bytes.","Catch MarshalException in custom write paths and report which value has the wrong width."],"exampleFix":"// before\nByteBuffer bad = ByteBuffer.allocate(4).putInt(42); // 4 bytes\n// after\nByteBuffer good = LongSerializer.instance.serialize(42L); // 8 bytes","handlingStrategy":"validation","validationCode":"// Java\npublic static void requireLongBytes(ByteBuffer v) {\n    int n = v == null ? 0 : v.remaining();\n    if (n != 8 && n != 0)\n        throw new IllegalArgumentException(\"bigint value must be 8 bytes or empty, got \" + n);\n}","typeGuard":"boolean isLongSized(ByteBuffer v) { int n = (v == null) ? 0 : v.remaining(); return n == 8 || n == 0; }","tryCatchPattern":"try { longSerializer.validate(value, accessor); } catch (MarshalException e) { throw new InvalidRequestException(\"Bad bigint value: \" + e.getMessage()); }","preventionTips":["Use LongSerializer.serialize(Long) instead of wrapping ints or blobs.","Check column types with DESCRIBE before programmatic writes.","For null values pass an empty buffer or null, never a partial buffer."],"tags":["serialization","type-mismatch","bigint"],"backgroundTag":"type-mismatch","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"}