{"record":{"id":"edbe9fb2e8390aea","repo":"apache/cassandra","slug":"not-enough-bytes-to-read-dth-field-s","errorCode":null,"errorMessage":"Not enough bytes to read %dth field %s","messagePattern":"Not enough bytes to read (.+?)th field (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/CQL3Type.java","lineNumber":424,"sourceCode":"                    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)));\n\n                ByteBuffer field = ByteBufferUtil.readBytes(buffer, size);\n                target.append(type.fieldType(i).asCQL3Type().toCQLLiteral(field));\n            }\n            target.append('}');\n            return target.toString();\n        }\n\n        @Override\n        public final boolean equals(Object o)\n        {\n            if(!(o instanceof UserDefined))\n                return false;\n\n            UserDefined that = (UserDefined)o;\n            return type.equals(that.type);\n        }\n","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/CQL3Type.java#L406-L442","documentation":"While rendering a UDT value as a CQL literal, after reading a field's 4-byte size the code requires that many bytes to actually contain the field value. If the buffer ends early, the payload is truncated and MarshalException is thrown naming the field index and name.","triggerScenarios":"A field size prefix says e.g. 16 bytes but only 5 remain in the ByteBuffer; passing a buffer that was sliced mid-field; corrupted serialized UDT data.","commonSituations":"Byte buffers truncated by custom transport or storage layers; application code miscounting serialized field lengths when composing UDT bytes manually.","solutions":["Ensure the serialized UDT value is complete: each field must carry its full size-prefixed payload","Re-produce the value via the official UserType serializer instead of manual byte assembly","Catch MarshalException and log/handle the corrupt value rather than crashing"],"exampleFix":"// before\nByteBuffer bad = ByteBuffer.allocate(4); bad.putInt(16); // claims 16 bytes, none follow\nString lit = udt.asCQL3Type().toCQLLiteral(bad);\n// after\nByteBuffer good = userType.serializer().serialize(udtValue);\nString lit = udt.asCQL3Type().toCQLLiteral(good);","handlingStrategy":"try-catch","validationCode":"ByteBuffer dup = buffer.duplicate(); // walk size prefixes\nwhile (dup.hasRemaining()) { if (dup.remaining() < 4) throw new MarshalException(\"truncated\"); int size = dup.getInt(); if (dup.remaining() < size) throw new MarshalException(\"field truncated\"); dup.position(dup.position() + size); }","typeGuard":null,"tryCatchPattern":"try { String lit = udtType.asCQL3Type().toCQLLiteral(buffer); } catch (MarshalException e) { handleCorruptValue(e); }","preventionTips":["Serialize UDTs only via UserType/TupleType APIs","Never slice serialized values at arbitrary offsets","Detect and quarantine corrupt cells instead of rendering them"],"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"}