{"record":{"id":"3cd3899b93593995","repo":"apache/druid","slug":"count-is-negative","errorCode":null,"errorMessage":"count is negative, ","messagePattern":"count is negative, ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java","lineNumber":608,"sourceCode":"   * <p>\n   * If count or length is zero then the empty string is returned.\n   * <p>\n   * This method may be used to create space padding for\n   * formatting text or zero padding for formatting numbers.\n   *\n   * @param count number of times to repeat\n   *\n   * @return A string composed of this string repeated\n   * {@code count} times or the empty string if count\n   * or length is zero.\n   *\n   * @throws IllegalArgumentException if the {@code count} is negative.\n   * @link https://bugs.openjdk.java.net/browse/JDK-8197594\n   */\n  public static String repeat(String s, int count)\n  {\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;","sourceCodeStart":590,"sourceCodeEnd":626,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java#L590-L626","documentation":"StringUtils.repeat(String, int) repeats a string a given number of times. It throws IllegalArgumentException when count is negative, mirroring the commons-lang behavior it replaces (JDK-8197594 workaround). A negative repeat count is meaningless and treated as a caller bug.","triggerScenarios":"Calling StringUtils.repeat(s, n) with n < 0 — typically when n comes from user input, computed padding widths (e.g. length difference), or unvalidated config.","commonSituations":"Formatting/padding code computing count as (targetLength - actualLength) when the string is already longer than target; SQL LPAD-like expressions with negative length parameters.","solutions":["Clamp the count before calling: Math.max(0, count).","Validate user/config-supplied lengths before passing them.","Catch IllegalArgumentException and return \"\" for negative counts.","Fix the computation so the difference is never negative (guard the subtraction)."],"exampleFix":"// before\nString pad = StringUtils.repeat(\" \", width - s.length());\n// after\nString pad = StringUtils.repeat(\" \", Math.max(0, width - s.length()));","handlingStrategy":"validation","validationCode":"if (count < 0) { throw new IllegalArgumentException(\"repeat count must be >= 0, got \" + count); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always clamp computed repeat counts with Math.max(0, n)","Validate user/config-supplied lengths before formatting","Guard padding math (targetLen - len) against negative results"],"tags":["string-utils","illegal-argument","negative-value"],"backgroundTag":"invalid-argument-value","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"}