{"record":{"id":"91dae4184587e496","repo":"apache/cassandra","slug":"unexpected-extraneous-bytes-after-map-value","errorCode":null,"errorMessage":"Unexpected extraneous bytes after map value","messagePattern":"Unexpected extraneous bytes after map value","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/MapSerializer.java","lineNumber":113,"sourceCode":"    {\n        if (accessor.isEmpty(input))\n            throw new MarshalException(\"Not enough bytes to read a map\");\n        try\n        {\n            int n = readCollectionSize(input, accessor);\n            int offset = sizeOfCollectionSize();\n            for (int i = 0; i < n; i++)\n            {\n                T key = readNonNullValue(input, accessor, offset);\n                offset += sizeOfValue(key, accessor);\n                keys.validate(key, accessor);\n\n                T value = readNonNullValue(input, accessor, offset);\n                offset += sizeOfValue(value, accessor);\n                values.validate(value, accessor);\n            }\n            if (!accessor.isEmptyFromOffset(input, offset))\n                throw new MarshalException(\"Unexpected extraneous bytes after map value\");\n        }\n        catch (BufferUnderflowException | IndexOutOfBoundsException e)\n        {\n            throw new MarshalException(\"Not enough bytes to read a map\");\n        }\n    }\n\n    @Override\n    public <I> Map<K, V> deserialize(I input, ValueAccessor<I> accessor)\n    {\n        try\n        {\n            int n = readCollectionSize(input, accessor);\n            int offset = sizeOfCollectionSize();\n\n            if (n < 0)\n                throw new MarshalException(\"The data cannot be deserialized as a map\");\n","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/MapSerializer.java#L95-L131","documentation":"After validating all declared key/value entries of a serialized map, MapSerializer.verify valid positions... actually MapSerializer.validate checks that no leftover bytes remain: if the buffer still has bytes after the last element, this MarshalException is thrown. It means the byte buffer contains more data than the map's declared structure consumes — trailing garbage or a value that is not a map.","triggerScenarios":"Validating a buffer whose declared element count times (4+len key) + (4+len value) is less than the buffer size — e.g. concatenating values, wrong serializer used (list bytes validated as a map with keys fitting), or corrupted cell with appended bytes.","commonSituations":"Passing frozen<list> bytes to a map type, blob literals that contain extra bytes, cells written by buggy code that appends metadata after the map.","solutions":["Use the correct type/serializer — confirm the column type matches the bytes being validated.","Trim the buffer to exactly the map's consumed bytes if extra bytes are expected by your producer.","Catch MarshalException and reject the write/mutation as invalid input.","If the data is stored on disk, treat as corruption and run scrub/repair."],"exampleFix":"// before\nmapType.validate(listBytes); // wrong type bytes\n// after\nmapType.validate(mapBytes); // validate with the matching serializer","handlingStrategy":"validation","validationCode":"// Java\n// Ensure the byte stream is fully consumed by the declared structure before validating:\nmapType.validate(buffer); // throws 'Unexpected extraneous bytes after map value' when trailing bytes exist","typeGuard":null,"tryCatchPattern":"try { mapType.validate(bytes); } catch (MarshalException e) { throw new InvalidRequestException(\"Value is not a valid map: \" + e.getMessage()); }","preventionTips":["Match the serializer to the column type; never validate list/set bytes with the map type.","Producers must emit exactly count + entries — no appended metadata.","Round-trip test: serialize then validate every custom write path."],"tags":["serialization","validation","collections"],"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-17T15:17:12.973Z"}