{"id":"fec07b7c7ff99257","repo":"apache/kafka","slug":"string-length-length-is-larger-than-the-maximum","errorCode":null,"errorMessage":"String length ${length} is larger than the maximum string length.","messagePattern":"String length (.+?) is larger than the maximum string length\\.","errorType":"exception","errorClass":"SchemaException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java","lineNumber":122,"sourceCode":"         */\n        public abstract String typeName();\n\n        /**\n         * Documentation of the Type.\n         *\n         * @return details about valid values, representation\n         */\n        public abstract String documentation();\n\n        @Override\n        public String toString() {\n            return typeName();\n        }\n    }\n\n    public static String stringRead(ByteBuffer buffer, int length) {\n        if (length > Short.MAX_VALUE)\n            throw new SchemaException(\"String length \" + length + \" is larger than the maximum string length.\");\n        if (length > buffer.remaining())\n            throw new SchemaException(\"Error reading string of length \" + length + \", only \" + buffer.remaining() + \" bytes available\");\n        String result = Utils.utf8(buffer, length);\n        buffer.position(buffer.position() + length);\n        return result;\n    }\n\n    public static ByteBuffer bytesRead(ByteBuffer buffer, int size) {\n        if (size > buffer.remaining())\n            throw new SchemaException(\"Error reading bytes of size \" + size + \", only \" + buffer.remaining() + \" bytes available\");\n\n        int limit = buffer.limit();\n        int newPosition = buffer.position() + size;\n        buffer.limit(newPosition);\n        ByteBuffer val = buffer.slice();\n        buffer.limit(limit);\n        buffer.position(newPosition);\n        return val;","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java#L104-L140","documentation":"Thrown by stringRead() when decoding a Kafka protocol STRING field whose declared length exceeds Short.MAX_VALUE (32767). The legacy STRING type encodes length as a signed INT16, so any value outside [-32768, 32767] is physically impossible to represent; this guard rejects malformed or truncated frames before allocating a giant buffer. It is a hard schema-validation failure raised as a SchemaException.","triggerScenarios":"Calling STRING.read(buffer) (or Schema.read on a buffer containing a STRING field) where the first two bytes decoded as a short exceed 32767; reached indirectly via ApiMessage deserialization, RequestMessage, or SendBuilder on a corrupt/length-mismatched ByteBuffer. Also hit by stringRead() invoked from COMPACT_STRING.read when the decoded varint length is oversized.","commonSituations":"Wire-level corruption from a partial write, network truncation, or reading a buffer whose position was not reset. Mismatched client/broker protocol versions where a newer field is interpreted as STRING length. Accidentally handing a non-Kafka payload (e.g. a TLS record or HTTP body) to a Kafka deserializer.","solutions":["Verify the byte buffer being decoded actually came from a Kafka protocol response/request at the expected API key and version.","Check that buffer.position()/limit() are correctly set before the read; dump the surrounding bytes to confirm the length prefix is sane.","Confirm client and broker versions are compatible for the API key in the failing exchange (enable DEBUG org.apache.kafka.common.protocol for the API key).","If the payload originates from your own serialization, re-encode it with the matching Schema rather than hand-built buffers."],"exampleFix":"// before: handing a raw truncated buffer to STRING.read\nshort len = buffer.getShort();\nString v = Type.stringRead(buffer, len); // throws if len > 32767\n\n// after: guard the length and surface a clearer error\nshort len = buffer.getShort();\nif (len < 0 || len > Short.MAX_VALUE || len > buffer.remaining())\n    throw new SchemaException(\"Malformed STRING field: length=\" + len\n        + \" remaining=\" + buffer.remaining());\nString v = Utils.utf8(buffer, len);","handlingStrategy":"validation","validationCode":"// stringRead(buffer, length) is public and takes length as a param.\n// Reject oversized lengths BEFORE calling it.\nif (length > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\n        \"String length \" + length + \" exceeds max \" + Short.MAX_VALUE);\n}\nString value = org.apache.kafka.common.protocol.types.Type.stringRead(buffer, length);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["stringRead is a public static helper that takes the already-decoded length as an argument; validate that argument against Short.MAX_VALUE (32767) before the call.","Treat any length close to 32767 as suspicious — it usually signals a corrupt or hostile frame; log and drop the buffer rather than proceeding.","If you do not control the buffer (e.g. data straight off the wire), wrap the read in try/catch SchemaException instead — validation only helps when you already hold the length field."],"tags":["protocol","serialization","schema","string","bytebuffer"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}