{"record":{"id":"5adfd14940c4d45a","repo":"apache/hadoop","slug":"illegal-initial-capacity-initcapacity","errorCode":null,"errorMessage":"Illegal initial capacity: \" + initCapacity","messagePattern":"Illegal initial capacity: \" \\+ initCapacity","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightResizableGSet.java","lineNumber":68,"sourceCode":"  static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;\n\n  /**\n   * The load factor used when none specified in constructor.\n   */\n  static final float DEFAULT_LOAD_FACTOR = 0.75f;\n\n  /** Size of the entry table. */\n  private int capacity;\n\n  /**\n   * The load factor for the hash set.\n   */\n  private final float loadFactor;\n  private int threshold;\n\n  public LightWeightResizableGSet(int initCapacity, float loadFactor) {\n    if (initCapacity < 0) {\n      throw new HadoopIllegalArgumentException(\"Illegal initial capacity: \" +\n          initCapacity);\n    }\n    if (loadFactor <= 0 || loadFactor > 1.0f) {\n      throw new HadoopIllegalArgumentException(\"Illegal load factor: \" +\n          loadFactor);\n    }\n    this.capacity = actualArrayLength(initCapacity);\n    this.hash_mask = capacity - 1;\n    this.loadFactor = loadFactor;\n    this.threshold = (int) (capacity * loadFactor);\n\n    entries = new LinkedElement[capacity];\n  }\n\n  public LightWeightResizableGSet() {\n    this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);\n  }\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightResizableGSet.java#L50-L86","documentation":"LightWeightResizableGSet is a chained, resizable hash set used by NameNode components. Its constructor follows the java.util.HashMap contract: the first argument, initCapacity, is the initial table-size hint, and a negative value throws HadoopIllegalArgumentException(\"Illegal initial capacity: N\") before any LinkedElement[] array is allocated.","triggerScenarios":"new LightWeightResizableGSet(-1, 0.75f); an initCapacity computed from arithmetic that underflows below zero (e.g. expectedSize - slack); tests enumerating invalid constructor arguments.","commonSituations":"Deriving the initial table size from a configuration value or size estimate and passing it straight through; copying HashMap-style code where the identical validation exists; a 'not set' -1 sentinel from config reaching the constructor.","solutions":["Pass initCapacity >= 0 (0 is legal and means the minimum table)","Clamp computed values: Math.max(0, expectedSize - slack)","Use the no-arg constructor (DEFAULT_INITIAL_CAPACITY / DEFAULT_LOAD_FACTOR) when the size is unknown"],"exampleFix":"// before\nnew LightWeightResizableGSet(estimated - RESERVED, 0.75f);\n\n// after\nnew LightWeightResizableGSet(Math.max(0, estimated - RESERVED), 0.75f);","handlingStrategy":"validation","validationCode":"int cap = Math.max(0, estimatedSize - RESERVED);\nnew LightWeightResizableGSet<>(cap, 0.75f);","typeGuard":null,"tryCatchPattern":"try { set = new LightWeightResizableGSet<>(cap, 0.75f); } catch (HadoopIllegalArgumentException e) { throw new IllegalArgumentException(\"Bad GSet init capacity \" + cap + \": \" + e.getMessage(), e); }","preventionTips":["Clamp any computed capacity with Math.max(0, ...) at the call site","Use the no-arg constructor when the size hint is unknown","Keep a unit test that feeds 0 and negative estimates through your sizing code"],"tags":["hadoop","java","gset","constructor","argument-validation"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}