{"record":{"id":"cd45c74cf657e4fc","repo":"apache/cassandra","slug":"object-o-maps-to-a-buffer-that-already-exists-i","errorCode":null,"errorMessage":"Object ${o} maps to a buffer that already exists in the set","messagePattern":"Object (.+?) maps to a buffer that already exists in the set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/utils/ByteBufferUtil.java","lineNumber":654,"sourceCode":"            Map<ByteBuffer, ByteBuffer> bbs = new LinkedHashMap<>();\n            for (Map.Entry<?, ?> e : map.entrySet())\n            {\n                Object key = e.getKey();\n                ByteBuffer previousValue = bbs.put(objectToBytes(key), objectToBytes(e.getValue()));\n                if (previousValue != null)\n                    throw new IllegalStateException(\"Key \" + key + \" already maps to value \" + previousValue);\n            }\n            // decompose/serializer doesn't use the isMultiCell, so safe to do this\n            return MapType.getInstance(BytesType.instance, BytesType.instance, false).decompose(bbs);\n        }\n        else if (obj instanceof Set)\n        {\n            Set<?> set = (Set<?>) obj;\n            // convert subtypes to BB\n            Set<ByteBuffer> bbs = new LinkedHashSet<>();\n            for (Object o : set)\n                if (!bbs.add(objectToBytes(o)))\n                    throw new IllegalStateException(\"Object \" + o + \" maps to a buffer that already exists in the set\");\n            // decompose/serializer doesn't use the isMultiCell, so safe to do this\n            return SetType.getInstance(BytesType.instance, false).decompose(bbs);\n        }\n        else if (obj instanceof Date)\n            return TimestampType.instance.decompose((Date) obj);\n        else\n            throw new IllegalArgumentException(String.format(\"Cannot convert value %s of type %s\",\n                                                             obj,\n                                                             obj.getClass()));\n    }\n\n    public static ByteBuffer bytes(byte b)\n    {\n        return ByteBuffer.allocate(1).put(0, b);\n    }\n\n    public static ByteBuffer bytes(short s)\n    {","sourceCodeStart":636,"sourceCodeEnd":672,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/ByteBufferUtil.java#L636-L672","documentation":"When converting a Set to ByteBuffers in objectToBytes, two distinct elements producing identical serialized buffers would silently collapse; the code detects this and throws IllegalStateException to preserve set cardinality semantics.","triggerScenarios":"objectToBytes is called on a Set containing two elements whose byte conversions are equal (e.g., Integer 1 and Long 1L, or objects with colliding custom serializers), typically during UDT value decomposition.","commonSituations":"Mixed-type sets in application code mapped to a UDT; custom codecs producing identical bytes for distinct elements; numerically equal values of different Java types.","solutions":["Deduplicate elements by their serialized form before building the set","Use a single consistent element type","Fix custom serializers that map distinct objects to the same bytes","Wrap the conversion in a validation step comparing element byte representations"],"exampleFix":"// before\nSet<Object> s = new HashSet<>(Arrays.asList(1, 1L)); // both -> same buffer\n// after\nSet<Object> s = new HashSet<>(Collections.singletonList(1L));","handlingStrategy":"validation","validationCode":"// detect byte-colliding set elements before conversion\nSet<ByteBuffer> seen = new HashSet<>();\nfor (Object o : set) {\n    if (!seen.add(ByteBufferUtil.objectToBytes(o).duplicate()))\n        throw new IllegalArgumentException(\"elements collide after serialization: \" + o);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use homogeneous element types in sets","Deduplicate elements by serialized bytes before UDT construction","Avoid mixed numeric types (Integer vs Long) in sets"],"tags":["serialization","collections","bytebuffer"],"backgroundTag":"internal-invariant-violation","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"}