{"record":{"id":"424177e1cd2f5eb7","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-size-of-dth-field-s","errorCode":null,"errorMessage":"Not enough bytes to read size of %dth field %s","messagePattern":"Not enough bytes to read size of (.+?)th field (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/CQL3Type.java","lineNumber":406,"sourceCode":"\n        @Override\n        public String toCQLLiteral(ByteBuffer buffer)\n        {\n            if (buffer == null)\n                return \"null\";\n\n\n            StringBuilder target = new StringBuilder();\n            buffer = buffer.duplicate();\n            target.append('{');\n            for (int i = 0; i < type.size(); i++)\n            {\n                // we allow the input to have less fields than declared so as to support field addition.\n                if (!buffer.hasRemaining())\n                    break;\n\n                if (buffer.remaining() < 4)\n                    throw new MarshalException(String.format(\"Not enough bytes to read size of %dth field %s\", i, type.fieldName(i)));\n\n                int size = buffer.getInt();\n\n                if (i > 0)\n                    target.append(\", \");\n\n                target.append(ColumnIdentifier.maybeQuote(type.fieldNameAsString(i)));\n                target.append(\": \");\n\n                // size < 0 means null value\n                if (size < 0)\n                {\n                    target.append(\"null\");\n                    continue;\n                }\n\n                if (buffer.remaining() < size)\n                    throw new MarshalException(String.format(\"Not enough bytes to read %dth field %s\", i, type.fieldName(i)));","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/CQL3Type.java#L388-L424","documentation":"When converting a serialized UserType (UDT) value to a CQL literal, CQL3Type.toCQLLiteral reads a 4-byte size prefix for each declared field. If fewer than 4 bytes remain where the next field's size should be, the buffer is corrupt/truncated and MarshalException is thrown naming the field index and name.","triggerScenarios":"Passing a truncated or manually corrupted ByteBuffer to toCQLLiteral for a UDT type; deserializing a value written by a different/broken serializer; off-by-one slicing of a serialized blob.","commonSituations":"Application code storing UDT blobs and re-rendering them after partial reads; data corruption on disk or in transit; protocol mismatches when bytes were hand-assembled.","solutions":["Verify the ByteBuffer contains a complete, uncorrupted serialized UDT value (check remaining() against the expected encoding)","Re-read or re-fetch the value — the stored data may be truncated","Use TypeSerializer/UTCMetaData-compliant serialization paths instead of hand-building byte buffers"],"exampleFix":"// before\nString literal = udtType.asCQL3Type().toCQLLiteral(sliceOf(valueBuffer, 0, 6)); // truncated\n// after\nString literal = udtType.asCQL3Type().toCQLLiteral(valueBuffer.duplicate()); // full value","handlingStrategy":"try-catch","validationCode":"if (buffer == null || buffer.remaining() < 4) throw new MarshalException(\"Buffer too small for UDT field\");","typeGuard":null,"tryCatchPattern":"try { String lit = udtType.asCQL3Type().toCQLLiteral(buffer); } catch (MarshalException e) { log.error(\"Corrupt UDT value: {}\", e.getMessage(), e); }","preventionTips":["Only pass buffers produced by the official UserType serializer","Check buffer.remaining() against expected field sizes before rendering","Treat stored blobs as untrusted and wrap literal rendering in try-catch"],"tags":["cassandra","cql","serialization","udt","bytebuffer"],"backgroundTag":"unexpected-response-shape","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"}