{"record":{"id":"04f793959047f5da","repo":"prestodb/presto","slug":"positioncount-is-negative-04f793","errorCode":null,"errorMessage":"positionCount is negative","messagePattern":"positionCount is negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/VariableWidthBlock.java","lineNumber":75,"sourceCode":"    @Nullable\n    private final boolean[] valueIsNull;\n\n    private final long retainedSizeInBytes;\n    private final long sizeInBytes;\n\n    public VariableWidthBlock(int positionCount, Slice slice, int[] offsets, Optional<boolean[]> valueIsNull)\n    {\n        this(0, positionCount, slice, offsets, valueIsNull.orElse(null));\n    }\n\n    VariableWidthBlock(int arrayOffset, int positionCount, Slice slice, int[] offsets, boolean[] valueIsNull)\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 (slice == null) {\n            throw new IllegalArgumentException(\"slice is null\");\n        }\n        this.slice = slice;\n\n        if (offsets.length - arrayOffset < (positionCount + 1)) {\n            throw new IllegalArgumentException(\"offsets length is less than positionCount\");\n        }\n        this.offsets = offsets;\n\n        if (valueIsNull != null && valueIsNull.length - arrayOffset < positionCount) {\n            throw new IllegalArgumentException(\"valueIsNull length is less than positionCount\");\n        }\n        this.valueIsNull = valueIsNull;\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/VariableWidthBlock.java#L57-L93","documentation":"VariableWidthBlock's constructor rejects a negative positionCount. A block cannot represent a negative number of positions; passing one indicates an arithmetic bug (e.g. end - start with end < start) in the calling code.","triggerScenarios":"Constructing VariableWidthBlock where positionCount < 0, commonly from computing count as an end position minus start position where the range is inverted or empty ranges are mis-handled as negative.","commonSituations":"Range slicing code with swapped start/end arguments; aggregation that subtracts lengths without guarding empty selections.","solutions":["Fix the computation so positionCount = end - start is never negative (check argument order).","Return an empty block (positionCount 0) for empty ranges instead of computing a negative count.","Validate range parameters before constructing the block."],"exampleFix":"// before\nint count = start - end; // inverted\nnew VariableWidthBlock(0, count, slice, offsets, null);\n// after\nint count = Math.max(0, end - start);\nnew VariableWidthBlock(0, count, slice, offsets, null);","handlingStrategy":"validation","validationCode":"int positionCount = end - start;\ncheckState(positionCount >= 0, \"inverted range: start=%s end=%s\", start, end);","typeGuard":null,"tryCatchPattern":"try {\n    new VariableWidthBlock(0, positionCount, slice, offsets, valueIsNull);\n} catch (IllegalArgumentException e) {\n    // fall back to empty block and log the bad range\n}","preventionTips":["Check start/end argument order in range computations","Return empty blocks for empty ranges instead of subtracting lengths","Guard aggregations that compute counts by subtraction"],"tags":["presto","block","illegal-argument","constructor-validation"],"backgroundTag":"block-constructor-validation","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"}