{"record":{"id":"4c31305907e1daa9","repo":"apache/hadoop","slug":"name-cannot-be-negative-but-was-value","errorCode":null,"errorMessage":"name + \" cannot be negative but was: \" + value","messagePattern":"name \\+ \" cannot be negative but was: \" \\+ value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Lists.java","lineNumber":199,"sourceCode":"   * @param elements elements.\n   * @param <E> Generics Type E.\n   * @return Generics Type E List.\n   */\n  public static <E> LinkedList<E> newLinkedList(\n      Iterable<? extends E> elements) {\n    LinkedList<E> list = newLinkedList();\n    addAll(list, elements);\n    return list;\n  }\n\n  private static int computeArrayListCapacity(int arraySize) {\n    checkNonnegative(arraySize, \"arraySize\");\n    return saturatedCast(5L + arraySize + (arraySize / 10));\n  }\n\n  private static int checkNonnegative(int value, String name) {\n    if (value < 0) {\n      throw new IllegalArgumentException(name + \" cannot be negative but was: \"\n          + value);\n    }\n    return value;\n  }\n\n  /**\n   * Returns the {@code int} nearest in value to {@code value}.\n   *\n   * @param value any {@code long} value.\n   * @return the same value cast to {@code int} if it is in the range of the\n   *     {@code int} type, {@link Integer#MAX_VALUE} if it is too large,\n   *     or {@link Integer#MIN_VALUE} if it is too small.\n   */\n  private static int saturatedCast(long value) {\n    if (value > Integer.MAX_VALUE) {\n      return Integer.MAX_VALUE;\n    }\n    if (value < Integer.MIN_VALUE) {","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Lists.java#L181-L217","documentation":"org.apache.hadoop.util.Lists is Hadoop's internal near-copy of Guava's Lists. Before sizing a backing array via computeArrayListCapacity (saturatedCast of 5L + arraySize + arraySize/10), checkNonnegative rejects a negative arraySize with IllegalArgumentException(\"arraySize cannot be negative but was: N\"). Zero is legal; only negatives throw.","triggerScenarios":"Lists.newArrayListWithCapacity(n) with n < 0 — typically n from an estimate that underflowed (expectedSize - reserved, size/2 - x) or a config-parsed capacity with a negative 'unset' sentinel.","commonSituations":"Copied Guava idioms resolving to Hadoop's Lists; expected-size arithmetic on empty or tiny inputs; forwarding a -1 default from an unset configuration key.","solutions":["Clamp the estimate before the call: Math.max(0, n)","Use newArrayListWithExpectedSize or plain newArrayList() when the capacity is unknown","Fix the upstream expression that can go negative and add a unit test for the empty-input case"],"exampleFix":"// before\nList<String> l = Lists.newArrayListWithCapacity(expected - RESERVE);\n\n// after\nList<String> l = Lists.newArrayListWithCapacity(Math.max(0, expected - RESERVE));","handlingStrategy":"validation","validationCode":"int size = Math.max(0, computeEstimate(...));\nList<String> list = size > 0 ? Lists.newArrayListWithCapacity(size) : Lists.<String>newArrayList();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp size arithmetic with Math.max(0, ...) before collection construction","Never pass config '-1 means unset' sentinels through to sizing APIs","Favor newArrayListWithExpectedSize when the value is an estimate, not a capacity"],"tags":["hadoop","java","collections","argument-validation"],"backgroundTag":"negative-size-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}