{"record":{"id":"81093b5eb0aab79d","repo":"prestodb/presto","slug":"initial-capacity-s-is-negative","errorCode":null,"errorMessage":"Initial capacity '%s' is negative","messagePattern":"Initial capacity '(.+?)' is negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/IntArrayList.java","lineNumber":33,"sourceCode":"\nimport java.util.Arrays;\n\nimport static com.facebook.presto.common.block.BlockUtil.MAX_ARRAY_SIZE;\nimport static java.lang.String.format;\n\n/**\n * A simplified version of fastutils IntArrayList for the purpose of positions copying.\n */\nclass IntArrayList\n{\n    private static final int DEFAULT_INITIAL_CAPACITY = 16;\n    private int[] array;\n    private int size;\n\n    IntArrayList(int initialCapacity)\n    {\n        if (initialCapacity < 0) {\n            throw new IllegalArgumentException(format(\"Initial capacity '%s' is negative\", initialCapacity));\n        }\n        array = new int[initialCapacity];\n    }\n\n    IntArrayList()\n    {\n        this(DEFAULT_INITIAL_CAPACITY);\n    }\n\n    int[] elements()\n    {\n        return array;\n    }\n\n    private void grow(int newCapacity)\n    {\n        if (array.length == MAX_ARRAY_SIZE) {\n            throw new IllegalStateException(\"Array reached maximum size\");","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/IntArrayList.java#L15-L51","documentation":"IntArrayList's package-private constructor validates the requested initial capacity and throws IllegalArgumentException when it is negative. A backing int[] cannot be allocated with a negative length, so the library fails fast before any allocation. This is a caller bug: the capacity value passed in is computed or configured incorrectly.","triggerScenarios":"Calling the IntArrayList(int initialCapacity) constructor with any value < 0, e.g. a size estimate that subtracted a larger value from a smaller one.","commonSituations":"Computing expected capacity from statistics or row counts that turned out negative (integer underflow, uninitialized counter, wrong unit conversion).","solutions":["Fix the calculation that produces the capacity so it cannot be negative","Clamp the capacity: int capacity = Math.max(0, estimatedSize)","Use the no-arg IntArrayList() constructor if no meaningful estimate exists"],"exampleFix":"// before\nIntArrayList list = new IntArrayList(expectedSize - otherSize);\n// after\nIntArrayList list = new IntArrayList(Math.max(0, expectedSize - otherSize));","handlingStrategy":"validation","validationCode":"if (initialCapacity < 0) {\n    throw new IllegalArgumentException(\"initialCapacity must be >= 0: \" + initialCapacity);\n}\nIntArrayList list = new IntArrayList(initialCapacity);","typeGuard":null,"tryCatchPattern":"try {\n    new IntArrayList(estimated);\n} catch (IllegalArgumentException e) {\n    // fall back to default capacity\n}","preventionTips":["Clamp computed capacities with Math.max(0, n)","Prefer the no-arg constructor when no reliable estimate exists","Beware integer subtraction/underflow in size estimates"],"tags":["illegal-argument","block","collections"],"backgroundTag":"negative-capacity","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}