{"record":{"id":"c896f4343c18792d","repo":"prestodb/presto","slug":"values-length-is-less-than-positioncount-c896f4","errorCode":null,"errorMessage":"values length is less than positionCount","messagePattern":"values length is less than positionCount","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/ShortArrayBlock.java","lineNumber":66,"sourceCode":"\n    public ShortArrayBlock(int positionCount, Optional<boolean[]> valueIsNull, short[] values)\n    {\n        this(0, positionCount, valueIsNull.orElse(null), values);\n    }\n\n    ShortArrayBlock(int arrayOffset, int positionCount, boolean[] valueIsNull, short[] values)\n    {\n        if (arrayOffset < 0) {\n            throw new IllegalArgumentException(\"arrayOffset is negative\");\n        }\n        this.arrayOffset = arrayOffset;\n        if (positionCount < 0) {\n            throw new IllegalArgumentException(\"positionCount is negative\");\n        }\n        this.positionCount = positionCount;\n\n        if (values.length - arrayOffset < positionCount) {\n            throw new IllegalArgumentException(\"values length is less than positionCount\");\n        }\n        this.values = values;\n\n        if (valueIsNull != null && valueIsNull.length - arrayOffset < positionCount) {\n            throw new IllegalArgumentException(\"isNull length is less than positionCount\");\n        }\n        this.valueIsNull = valueIsNull;\n\n        retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);\n    }\n\n    @Override\n    public long getSizeInBytes()\n    {\n        return SIZE_IN_BYTES_PER_POSITION * (long) positionCount;\n    }\n\n    @Override","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/ShortArrayBlock.java#L48-L84","documentation":"ShortArrayBlock's constructor checks that the backing short[] values array is at least arrayOffset + positionCount long. If values.length - arrayOffset < positionCount the block would read out of bounds, so the constructor rejects it with IllegalArgumentException. This protects all position reads against buffer overruns.","triggerScenarios":"Constructing ShortArrayBlock where values.length - arrayOffset < positionCount, e.g. passing a truncated array or an oversized positionCount.","commonSituations":"Slicing code that splits an array by positions but sizes the new array too small, aggregation of multiple segments where lengths were summed incorrectly, or corrupted serialized input.","solutions":["Size the values array to at least arrayOffset + positionCount before construction.","Recompute positionCount as values.length - arrayOffset when intending to consume the whole array.","Check serialization/deserialization of the array for truncation."],"exampleFix":"// before\nshort[] values = new short[positionCount - 1]; // too small\nnew ShortArrayBlock(arrayOffset, positionCount, values, valueIsNull);\n// after\nshort[] values = new short[arrayOffset + positionCount];\nnew ShortArrayBlock(arrayOffset, positionCount, values, valueIsNull);","handlingStrategy":"validation","validationCode":"if (values.length - arrayOffset < positionCount) {\n    throw new IllegalArgumentException(\"values too small: need \" + (arrayOffset + positionCount) + \", have \" + values.length);\n}","typeGuard":"boolean valuesFit(int arrayOffset, int positionCount, short[] values) {\n    return values != null && values.length - arrayOffset >= positionCount;\n}","tryCatchPattern":"try {\n    block = new ShortArrayBlock(arrayOffset, positionCount, values, valueIsNull);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"values length is less than positionCount\")) {\n        values = Arrays.copyOf(values, arrayOffset + positionCount);\n        block = new ShortArrayBlock(arrayOffset, positionCount, values, valueIsNull);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Size backing arrays as arrayOffset + positionCount in one place (a factory helper).","Never slice arrays by hand without recomputing lengths; prefer Block.getRegion.","Add assertions (checkArgument) before constructing blocks in custom code."],"tags":["presto","block","illegal-argument","array-length"],"backgroundTag":"array-length-mismatch","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"}