{"record":{"id":"4bf4ebe9491aeaeb","repo":"apache/druid","slug":"size-d-should-be-between-0-and-d","errorCode":null,"errorMessage":"Size[%d] should be between 0 and %d","messagePattern":"Size\\[(.+?)\\] should be between 0 and (.+?)","errorType":"exception","errorClass":"IAE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/segment/data/ArrayBasedIndexedInts.java","lineNumber":69,"sourceCode":"    }\n  }\n\n  public void setSize(int size)\n  {\n    if (size < 0 || size > expansion.length) {\n      throw new IAE(\"Size[%d] > expansion.length[%d] or < 0\", size, expansion.length);\n    }\n    this.size = size;\n  }\n\n  /**\n   * Sets the values from the given array. The given values array is not reused and not prone to be mutated later.\n   * Instead, the values from this array are copied into an array which is internal to ArrayBasedIndexedInts.\n   */\n  public void setValues(int[] values, int size)\n  {\n    if (size < 0 || size > values.length) {\n      throw new IAE(\"Size[%d] should be between 0 and %d\", size, values.length);\n    }\n    ensureSize(size);\n    System.arraycopy(values, 0, expansion, 0, size);\n    this.size = size;\n  }\n\n  public void setValue(int index, int value)\n  {\n    expansion[index] = value;\n  }\n\n  @Override\n  public int size()\n  {\n    return size;\n  }\n\n  @Override","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/segment/data/ArrayBasedIndexedInts.java#L51-L87","documentation":"ArrayBasedIndexedInts.setValues(int[], int) copies `size` elements from the given array into the internal expansion array. It throws IAE when size is negative or larger than the source array length, since copying would read out of bounds.","triggerScenarios":"Calling setValues(values, size) where size < 0 or size > values.length, typically passing a default/oversized size with a shorter array.","commonSituations":"Using a stale or wrongly computed row length with a freshly allocated values array; misuse during column initialization (initColumnValues).","solutions":["Pass values.length (or a correctly computed count) as size","Clamp size to [0, values.length] before calling","Check that the array actually contains the expected number of entries"],"exampleFix":"// before\nindexed.setValues(arr, expectedSize);\n// after\nindexed.setValues(arr, Math.min(arr.length, Math.max(0, expectedSize)));","handlingStrategy":"validation","validationCode":"int safeSize = Math.min(Math.max(size, 0), values.length); indexed.setValues(values, safeSize);","typeGuard":"boolean validValues(int[] values, int size) { return values != null && size >= 0 && size <= values.length; }","tryCatchPattern":"try { indexed.setValues(values, size); } catch (IAE e) { indexed.setValues(values, values.length); }","preventionTips":["Derive size from the actual array contents, not assumptions","Null/length-check source arrays before copying","Centralize ArrayBasedIndexedInts construction behind helpers that validate"],"tags":["java","index-out-of-range","segments"],"backgroundTag":"value-out-of-range","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}