{"record":{"id":"3db53b438af73e71","repo":"oracle/graal","slug":"string-too-long-to-encode-s-bytes","errorCode":null,"errorMessage":"String too long to encode, %s bytes","messagePattern":"String too long to encode, (.+?) bytes","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/truffle/BinaryOutput.java","lineNumber":197,"sourceCode":"    public final void writeUTF(String string) throws IllegalArgumentException {\n        int len = string.length();\n        long utfLen = 0;\n        int c;\n        int count = 0;\n\n        for (int i = 0; i < len; i++) {\n            c = string.charAt(i);\n            if ((c >= 0x0001) && (c <= 0x007F)) {\n                utfLen++;\n            } else if (c > 0x07FF) {\n                utfLen += 3;\n            } else {\n                utfLen += 2;\n            }\n        }\n\n        if (utfLen > MAX_LENGTH) {\n            throw new IllegalArgumentException(\"String too long to encode, \" + utfLen + \" bytes\");\n        }\n        int headerSize;\n        if (utfLen > MAX_SHORT_LENGTH) {\n            headerSize = Integer.BYTES;\n            ensureBufferSize(headerSize, (int) utfLen);\n            tempDecodingBuffer[count++] = (byte) ((LARGE_STRING_TAG | (utfLen >>> 24)) & 0xff);\n            tempDecodingBuffer[count++] = (byte) ((utfLen >>> 16) & 0xFF);\n        } else {\n            headerSize = Short.BYTES;\n            ensureBufferSize(headerSize, (int) utfLen);\n        }\n        tempDecodingBuffer[count++] = (byte) ((utfLen >>> 8) & 0xFF);\n        tempDecodingBuffer[count++] = (byte) (utfLen & 0xFF);\n\n        int i = 0;\n        for (; i < len; i++) {\n            c = string.charAt(i);\n            if (!((c >= 0x0001) && (c <= 0x007F))) {","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/truffle/BinaryOutput.java#L179-L215","documentation":"BinaryOutput.writeUTF first walks the string to compute the modified-UTF-8 byte length (1 byte for U+0001..U+007F, 2 for U+0080..U+07FF and NUL/surrogates, 3 above). If the total exceeds MAX_LENGTH it throws IllegalArgumentException('String too long to encode, %d bytes') before writing anything. The wire format's length header (short, or the LARGE_STRING_TAG int header) cannot represent more than MAX_LENGTH bytes.","triggerScenarios":"Marshalling a string whose encoded length exceeds the protocol's MAX_LENGTH across the libgraal/Truffle boundary via writeUTF — e.g. serializing source code, generated code text, or a large identifier payload as one string.","commonSituations":"Sending whole files, big generated snippets, or deeply nested stringified ASTs through the binary channel instead of chunking; tests with oversized synthetic strings after a format change lowered MAX_LENGTH.","solutions":["Chunk the payload: split into segments under MAX_LENGTH, write count + segments, and reassemble on read.","Pass large text out-of-band (e.g. via an object handle or file) and send only a reference.","Compute the encoded length up front with the same 1/2/3-byte rule and reject oversized inputs early.","Compress the string before marshalling if it is compressible and must stay inline."],"exampleFix":"// before\nout.writeUTF(hugeSource);\n// after (chunked transfer)\nint CHUNK = 1 << 16;\nfor (int i = 0; i < hugeSource.length(); i += CHUNK) {\n    out.writeUTF(hugeSource.substring(i, Math.min(hugeSource.length(), i + CHUNK)));\n}","handlingStrategy":"validation","validationCode":"// Compute encoded size with the same rule as writeUTF before sending\nlong utfLen = 0;\nfor (int i = 0; i < s.length(); i++) {\n    char c = s.charAt(i);\n    utfLen += (c >= 0x0001 && c <= 0x007F) ? 1 : (c > 0x07FF ? 3 : 2);\n}\nif (utfLen > MAX_LENGTH) { /* chunk or send out-of-band */ }","typeGuard":null,"tryCatchPattern":"try {\n    out.writeUTF(s);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"String too long to encode\")) {\n        // fall back to chunked transfer of substrings\n    }\n}","preventionTips":["Never marshal unbounded strings inline; chunk by encoded size","Send large payloads out-of-band via handles or files","Bound string inputs at the API boundary"],"tags":["libgraal","truffle","marshalling","utf-8","size-limit"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}