{"record":{"id":"bed4b29a69bb00a8","repo":"apache/iceberg","slug":"encoded-string-is-too-long","errorCode":null,"errorMessage":"Encoded string is too long: ","messagePattern":"Encoded string is too long: ","errorType":"exception","errorClass":"UTFDataFormatException","httpStatus":null,"severity":"error","filePath":"flink/v1.20/flink/src/main/java/org/apache/iceberg/flink/util/SerializerHelper.java","lineNumber":70,"sourceCode":"   * @param str the string value to be written.\n   */\n  public static void writeLongUTF(DataOutputView out, String str) throws IOException {\n    int strlen = str.length();\n    long utflen = 0;\n    int ch;\n\n    /* use charAt instead of copying String to char array */\n    for (int i = 0; i < strlen; i++) {\n      ch = str.charAt(i);\n      utflen += getUTFBytesSize(ch);\n\n      if (utflen > Integer.MAX_VALUE) {\n        throw new UTFDataFormatException(\"Encoded string reached maximum length: \" + utflen);\n      }\n    }\n\n    if (utflen > Integer.MAX_VALUE - 4) {\n      throw new UTFDataFormatException(\"Encoded string is too long: \" + utflen);\n    }\n\n    out.writeInt((int) utflen);\n    writeUTFBytes(out, str, (int) utflen);\n  }\n\n  /**\n   * Similar to {@link DataInputDeserializer#readUTF()}. Except this supports larger payloads which\n   * is up to max integer value.\n   *\n   * <p>Note: This method can be removed when the method which does similar thing within the {@link\n   * DataOutputSerializer} already which does the same thing, so use that one instead once that is\n   * released on Flink version 1.20.\n   *\n   * <p>See * <a href=\"https://issues.apache.org/jira/browse/FLINK-34228\">FLINK-34228</a> * <a\n   * href=\"https://github.com/apache/flink/pull/24191\">https://github.com/apache/flink/pull/24191</a>\n   *\n   * @param in the input stream to read the string from.","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v1.20/flink/src/main/java/org/apache/iceberg/flink/util/SerializerHelper.java#L52-L88","documentation":"writeLongUTF reserves 4 bytes of headroom: after encoding, if the total length exceeds Integer.MAX_VALUE - 4 it throws UTFDataFormatException. This check guarantees the encoded length plus any small framing the caller adds still fits in an int when written via out.writeInt((int) utflen).","triggerScenarios":"Calling SerializerHelper.writeLongUTF with a String whose modified-UTF-8 encoded size is greater than Integer.MAX_VALUE - 4 bytes (~2147483643).","commonSituations":"Extreme single-record sizes in Flink serialization; passing unbounded user input through a serialized state or checkpoint path.","solutions":["Reduce the payload size (chunk, truncate, or compress) before serializing","Validate string length before calling writeLongUTF","Split into multiple records/parts so each encoded segment stays under the limit"],"exampleFix":"// before\nhelper.writeLongUTF(value);\n// after\nif (estimateUTFLength(value) <= Integer.MAX_VALUE - 4) {\n  helper.writeLongUTF(value);\n} else {\n  writeChunked(helper, value);\n}","handlingStrategy":"validation","validationCode":"static final int MAX_ENCODED = Integer.MAX_VALUE - 4;\nif (estimateModifiedUtf8Length(s) > MAX_ENCODED) {\n  throw new IllegalArgumentException(\"Encoded string exceeds \" + MAX_ENCODED + \" bytes\");\n}","typeGuard":"boolean withinLimit(String s) {\n  return s != null && estimateModifiedUtf8Length(s) <= Integer.MAX_VALUE - 4;\n}","tryCatchPattern":"try {\n  helper.writeLongUTF(str);\n} catch (UTFDataFormatException e) {\n  log.error(\"Payload too large for long-UTF serializer\");\n  writeChunked(helper, str);\n}","preventionTips":["Leave headroom for framing; enforce a max payload smaller than Integer.MAX_VALUE - 4","Split oversized strings at the application layer","Add length assertions in tests with max-size fixtures"],"tags":["flink","serialization","utf-8","size-limit"],"backgroundTag":"value-out-of-range","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}