{"record":{"id":"6655b242512573a4","repo":"prestodb/presto","slug":"invalid-position-s-and-length-s-in-block-with-s","errorCode":null,"errorMessage":"Invalid position %s and length %s in block with %s positions","messagePattern":"Invalid position (.+?) and length (.+?) in block with (.+?) positions","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/BlockUtil.java","lineNumber":61,"sourceCode":"    {\n        requireNonNull(array, \"array is null\");\n        if (offset < 0 || length < 0 || offset + length > array.length) {\n            throw new IndexOutOfBoundsException(format(\"Invalid offset %s and length %s in array with %s elements\", offset, length, array.length));\n        }\n    }\n\n    static void checkArrayRange(boolean[] array, int offset, int length)\n    {\n        requireNonNull(array, \"array is null\");\n        if (offset < 0 || length < 0 || offset + length > array.length) {\n            throw new IndexOutOfBoundsException(format(\"Invalid offset %s and length %s in array with %s elements\", offset, length, array.length));\n        }\n    }\n\n    static void checkValidRegion(int positionCount, int positionOffset, int length)\n    {\n        if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) {\n            throw new IndexOutOfBoundsException(format(\"Invalid position %s and length %s in block with %s positions\", positionOffset, length, positionCount));\n        }\n    }\n\n    static void checkValidPositions(boolean[] positions, int positionCount)\n    {\n        if (positions.length != positionCount) {\n            throw new IllegalArgumentException(format(\"Invalid positions array size %d, actual position count is %d\", positions.length, positionCount));\n        }\n    }\n\n    static void checkValidPosition(int position, int positionCount)\n    {\n        if (position < 0 || position >= positionCount) {\n            throw new IllegalArgumentException(format(\"Invalid position %s in block with %s positions\", position, positionCount));\n        }\n    }\n\n    static void checkValidSliceRange(int sourceIndex, int length)","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/BlockUtil.java#L43-L79","documentation":"BlockUtil.checkValidRegion validates a region view (positionOffset, length) against a block's positionCount, throwing IndexOutOfBoundsException when positionOffset is negative, length is negative, or positionOffset+length exceeds positionCount. It guards Block.getRegion-style slicing so no region references positions outside the block.","triggerScenarios":"Calling Block.getRegion (or a custom block that calls checkValidRegion) with a positionOffset or length computed from bad row math — e.g. requesting more positions than the block holds, or offset+length overflowing int.","commonSituations":"Paging/streaming code that assumes blocks always carry a fixed position count; cursor arithmetic bugs after partial reads; custom Block subclasses passing regionLength defaults larger than the block.","solutions":["Clamp positionOffset/length to the block's getPositionCount() before slicing","Use getRegionLength()/getSliceLength to derive length instead of hardcoding","Catch IndexOutOfBoundsException and surface which block and offsets were invalid"],"exampleFix":"// before\nBlock region = block.getRegion(offset, remaining);\n// after\nint len = Math.min(remaining, block.getPositionCount() - offset);\nBlock region = block.getRegion(offset, len);","handlingStrategy":"validation","validationCode":"if (positionOffset < 0 || length < 0 || positionOffset > positionCount - length) {\n    throw new IllegalArgumentException(\"bad region: offset=\" + positionOffset + \" length=\" + length + \" positions=\" + positionCount);\n}","typeGuard":"boolean isValidRegion(int positionCount, int positionOffset, int length) {\n    return positionOffset >= 0 && length >= 0 && positionOffset <= positionCount - length;\n}","tryCatchPattern":"try {\n    region = block.getRegion(positionOffset, length);\n} catch (IndexOutOfBoundsException e) {\n    throw new PrestoException(GENERIC_INTERNAL_ERROR, \"invalid region \" + positionOffset + \"+\" + length + \" of \" + block.getPositionCount(), e);\n}","preventionTips":["Clamp requested length against block.getPositionCount() before slicing","Derive region bounds from the block API (getRegionLength) rather than cached sizes","Add assertions on offset math in custom Block implementations"],"tags":["java","presto","index-out-of-bounds","block-region"],"backgroundTag":"array-index-out-of-bounds","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"}