{"record":{"id":"b3c56294ffdbbd28","repo":"apache/druid","slug":"the-produced-string-is-too-large","errorCode":null,"errorMessage":"The produced string is too large.","messagePattern":"The produced string is too large\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java","lineNumber":624,"sourceCode":"  {\n    if (count < 0) {\n      throw new IllegalArgumentException(\"count is negative, \" + count);\n    }\n    if (count == 1) {\n      return s;\n    }\n    byte[] value = s.getBytes(StandardCharsets.UTF_8);\n    final int len = value.length;\n    if (len == 0 || count == 0) {\n      return \"\";\n    }\n    if (len == 1) {\n      final byte[] single = new byte[count];\n      Arrays.fill(single, value[0]);\n      return new String(single, StandardCharsets.UTF_8);\n    }\n    if (Integer.MAX_VALUE / count < len) {\n      throw new RuntimeException(\"The produced string is too large.\");\n    }\n    final int limit = len * count;\n    final byte[] multiple = new byte[limit];\n    System.arraycopy(value, 0, multiple, 0, len);\n    int copied = len;\n    for (; copied < limit - copied; copied <<= 1) {\n      System.arraycopy(multiple, 0, multiple, copied, copied);\n    }\n    System.arraycopy(multiple, 0, multiple, copied, limit - copied);\n    return new String(multiple, StandardCharsets.UTF_8);\n  }\n\n  /**\n   * Returns the string left-padded with the string pad to a length of len characters.\n   * If str is longer than len, the return value is shortened to len characters.\n   * This function is migrated from flink's scala function with minor refactor\n   * https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala\n   * - Modified to handle empty pad string.","sourceCodeStart":606,"sourceCodeEnd":642,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java#L606-L642","documentation":"StringUtils.repeat(String, int) builds the repeated string as a UTF-8 byte array. Before allocating len*count bytes it checks whether that would exceed Integer.MAX_VALUE and throws RuntimeException \"The produced string is too large.\" to avoid overflow or impossible allocations.","triggerScenarios":"Calling StringUtils.repeat(s, count) where s.length() * count would exceed Integer.MAX_VALUE characters — e.g. repeating a non-trivial string millions of times, or an unbounded multiplier from user input/SQL expressions.","commonSituations":"SQL string functions (REPEAT/LPAD/SPACE) with huge or unvalidated count parameters; runaway expressions producing enormous padding values.","solutions":["Validate the multiplier and cap it so len * count stays well below Integer.MAX_VALUE (and realistic memory limits).","Compute the intended size up front and refuse/refuse-with-default if it is unreasonable.","Catch RuntimeException from repeat and return a truncated or empty value.","Fix the expression/query logic producing the huge count."],"exampleFix":"// before\nString s = StringUtils.repeat(\"x\", count);\n// after\nif (count < 0 || (long) count > (Integer.MAX_VALUE / Math.max(1, \"x\".length()))) {\n  throw new IllegalArgumentException(\"repeat count too large: \" + count);\n}\nString s = StringUtils.repeat(\"x\", count);","handlingStrategy":"validation","validationCode":"long total = (long) s.length() * (long) count;\nif (count < 0 || total > Integer.MAX_VALUE) { throw new IllegalArgumentException(\"repeat result too large: \" + total); }","typeGuard":null,"tryCatchPattern":"try { return StringUtils.repeat(s, count); } catch (RuntimeException e) { LOG.warn(e, \"repeat too large\"); return s; }","preventionTips":["Compute len*count as long before deciding to repeat","Cap user/query-supplied repeat counts with a sane maximum","Beware SQL REPEAT/LPAD expressions with unbounded multipliers"],"tags":["string-utils","memory-limit","overflow"],"backgroundTag":"payload-too-large","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}