{"record":{"id":"e662b75b8379fcff","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-a-set","errorCode":null,"errorMessage":"Not enough bytes to read a set","messagePattern":"Not enough bytes to read a set","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/SetSerializer.java","lineNumber":74,"sourceCode":"        this.elements = elements;\n        this.comparators = comparators;\n    }\n\n    @Override\n    public List<ByteBuffer> serializeValues(Set<T> values)\n    {\n        List<ByteBuffer> buffers = new ArrayList<>(values.size());\n        for (T value : values)\n            buffers.add(elements.serialize(value));\n        buffers.sort(comparators.buffer);\n        return buffers;\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 set\");\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            if (!accessor.isEmptyFromOffset(input, offset))\n                throw new MarshalException(\"Unexpected extraneous bytes after set value\");\n        }\n        catch (BufferUnderflowException | IndexOutOfBoundsException e)\n        {\n            throw new MarshalException(\"Not enough bytes to read a set\");\n        }\n    }","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/SetSerializer.java#L56-L92","documentation":"SetSerializer.validate rejects an empty (zero-byte) value because a valid serialized set must contain at least the collection-size prefix. Cassandra historically serializes empty collections as null; a zero-length buffer is therefore treated as malformed rather than as an empty set. This guard runs during validation of values being written or bound.","triggerScenarios":"Binding a zero-length ByteBuffer to a set column (e.g. ByteBufferUtil.EMPTY_BYTE_BUFFER) via execute/bound statements, or validating an empty buffer in a custom type test such as setSerDerTest.","commonSituations":"Client drivers or app code constructing an empty set value as an empty buffer instead of null; migrations that write empty collections; custom serialization tests feeding empty input.","solutions":["Pass null instead of an empty buffer to unset/delete the set, or pass a properly encoded set of the elements you want.","If the intent is to store an empty set, encode it with SetSerializer.serialize(Collections.emptySet()) (4-byte zero count) rather than a zero-length buffer.","In test code, use the serializer's own serialize method to build valid inputs."],"exampleFix":"// before\nboundStatement.setBytes(\"tags\", ByteBuffer.allocate(0));\n// after\nboundStatement.setToNull(\"tags\"); // or\nboundStatement.setSet(\"tags\", Collections.emptySet(), String.class);","handlingStrategy":"validation","validationCode":"// Java\nif (buf == null || buf.remaining() == 0) {\n    // treat as unset; bind null instead\n} else {\n    setSerializer.validate(buf.duplicate(), ByteBufferAccessor.instance);\n}","typeGuard":null,"tryCatchPattern":"try {\n    setSerializer.validate(buf, ByteBufferAccessor.instance);\n} catch (MarshalException e) {\n    throw new IllegalArgumentException(\"Invalid set value: \" + e.getMessage(), e);\n}","preventionTips":["Represent unset/empty collections as null, not zero-length buffers","Use setSerializer.serialize(Collections.emptySet()) for explicit empty sets","Bind typed collections via driver APIs instead of raw bytes"],"tags":["cassandra","serialization","set","validation"],"backgroundTag":"empty-required-field","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"}