{"record":{"id":"c3ce97491527a65d","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-a-list","errorCode":null,"errorMessage":"Not enough bytes to read a list","messagePattern":"Not enough bytes to read a list","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/ListSerializer.java","lineNumber":73,"sourceCode":"    private ListSerializer(TypeSerializer<T> elements)\n    {\n        this.elements = elements;\n    }\n\n    @Override\n    protected List<ByteBuffer> serializeValues(List<T> values)\n    {\n        List<ByteBuffer> output = new ArrayList<>(values.size());\n        for (T value: values)\n            output.add(elements.serialize(value));\n        return output;\n    }\n\n    @Override\n    public <V> void validate(V input, ValueAccessor<V> accessor)\n    {\n        if (accessor.isEmpty(input))\n            throw new MarshalException(\"Not enough bytes to read a list\");\n        try\n        {\n            int n = readCollectionSize(input, accessor);\n            int offset = sizeOfCollectionSize();\n            for (int i = 0; i < n; i++)\n            {\n                V value = readNonNullValue(input, accessor, offset);\n                offset += sizeOfValue(value, accessor);\n                elements.validate(value, accessor);\n            }\n\n            if (!accessor.isEmptyFromOffset(input, offset))\n                throw new MarshalException(\"Unexpected extraneous bytes after list value\");\n        }\n        catch (BufferUnderflowException | IndexOutOfBoundsException e)\n        {\n            throw new MarshalException(\"Not enough bytes to read a list\");\n        }","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/ListSerializer.java#L55-L91","documentation":"ListSerializer.validate() throws this when the input buffer is completely empty, because a serialized list must contain at least the collection-size count. An empty buffer cannot even be read as a count, so validation fails immediately. Note that an empty list is represented by a size of 0 with bytes present — not by zero bytes.","triggerScenarios":"Calling ListSerializer.validate(input, accessor) (directly or via ListType column validation) with an empty (0-byte) buffer; e.g. writing ByteBufferUtil.EMPTY_BYTE_BUFFER or null-serialized bytes into a list column.","commonSituations":"Application serializes an empty Java List as an empty buffer instead of a zero-count collection; a driver binds null differently across versions (pre-3.0 empty bytes vs null); ETL tools writing empty blobs into list columns; upgrading from older Cassandra versions where empty collections were allowed/represented differently.","solutions":["Serialize even empty lists with their element count (a 4-byte zero) using ListSerializer.serialize / the driver's list codec, not an empty buffer.","Bind null (unset) instead of an empty buffer when the intent is 'no value'.","If the column can legitimately hold no list, consider allowing null and skipping the write.","Check for code paths that coerce null to ByteBufferUtil.EMPTY_BYTE_BUFFER before binding."],"exampleFix":"// before\nByteBuffer bytes = ByteBufferUtil.EMPTY_BYTE_BUFFER; // empty blob\n// after\nByteBuffer bytes = ListSerializer.instance.serialize(Collections.emptyList(), ByteBufferUtil.NONE); // encodes count=0","handlingStrategy":"validation","validationCode":"if (listBytes == null || listBytes.remaining() == 0) { listBytes = ListSerializer.instance.serialize(Collections.emptyList(), Int32Type.instance, ByteBufferUtil.NONE); }","typeGuard":"boolean isNonEmptyListBlob(java.nio.ByteBuffer b) { return b != null && b.remaining() >= 4; }","tryCatchPattern":"try { listType.validate(buf, accessor); } catch (org.apache.cassandra.exceptions.MarshalException e) { log.warn(\"empty/malformed list blob, treating as null\"); return null; }","preventionTips":["Encode empty lists with a zero count header, never as a 0-byte buffer","Bind null (unset) rather than EMPTY_BYTE_BUFFER when meaning 'no value'","Audit any code that converts null collections to empty buffers"],"tags":["cassandra","serialization","collections","list"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}