{"record":{"id":"413a9ff07c6be7f8","repo":"prestodb/presto","slug":"cannot-allocate-slice-larger-than-d-bytes","errorCode":null,"errorMessage":"Cannot allocate slice larger than %d bytes","messagePattern":"Cannot allocate slice larger than (.+?) bytes","errorType":"exception","errorClass":"SliceTooLargeException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/BlockUtil.java","lineNumber":83,"sourceCode":"    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)\n    {\n        //sourceIndex + length can overflow integer range\n        if (sourceIndex > MAX_ARRAY_SIZE - length) {\n            throw new SliceTooLargeException(format(\"Cannot allocate slice larger than %d bytes\", MAX_ARRAY_SIZE));\n        }\n    }\n\n    static int calculateNewArraySize(int currentSize)\n    {\n        // grow array by 50%\n        long newSize = (long) currentSize + (currentSize >> 1);\n\n        // verify new size is within reasonable bounds\n        if (newSize < DEFAULT_CAPACITY) {\n            newSize = DEFAULT_CAPACITY;\n        }\n        else if (newSize > MAX_ARRAY_SIZE) {\n            newSize = MAX_ARRAY_SIZE;\n            if (newSize == currentSize) {\n                throw new IllegalArgumentException(format(\"Can not grow array beyond '%s'\", MAX_ARRAY_SIZE));\n            }\n        }","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/BlockUtil.java#L65-L101","documentation":"BlockUtil.checkValidSliceRange guards against allocating a Slice larger than MAX_ARRAY_SIZE when extracting length bytes starting at sourceIndex, throwing SliceTooLargeException because sourceIndex+length can overflow int and JVM arrays are capped near Integer.MAX_VALUE. It prevents oversized or overflowed allocation requests from reaching the VM.","triggerScenarios":"Requesting a slice/copy whose sourceIndex + length exceeds MAX_ARRAY_SIZE (or overflows int), typically from an unvalidated length field in serialized data or a bad variable-width column offset computation.","commonSituations":"Deserializing corrupt or hostile serialized blocks where declared lengths are huge; buggy VARCHAR/VARBINARY offset tables whose last offset exceeds the slice capacity; accumulating unbounded concatenations.","solutions":["Validate length against MAX_ARRAY_SIZE (and remaining source bytes) before requesting the slice","Fix the offset/length table so final offsets never exceed the underlying slice size","Catch SliceTooLargeException and fail the operation with a data-corruption/too-large error instead of retrying"],"exampleFix":"// before\nSlice value = slice.getBytes(0, declaredLength);\n// after\ncheckState(declaredLength >= 0 && declaredLength <= slice.length(), \"invalid length\");\nSlice value = slice.getBytes(0, Math.min(declaredLength, slice.length()));","handlingStrategy":"try-catch","validationCode":"if (length < 0 || sourceIndex > BlockUtil.MAX_ARRAY_SIZE - length) {\n    throw new PrestoException(GENERIC_INTERNAL_ERROR, \"slice too large: \" + length + \" bytes at \" + sourceIndex);\n}","typeGuard":"boolean isValidSliceLength(long length) {\n    return length >= 0 && length <= MAX_ARRAY_SIZE;\n}","tryCatchPattern":"try {\n    return checkValidSliceRange(sourceIndex, length);\n} catch (SliceTooLargeException e) {\n    throw new PrestoException(GENERIC_INTERNAL_ERROR, \"requested slice exceeds \" + MAX_ARRAY_SIZE + \" bytes\", e);\n}","preventionTips":["Validate declared lengths in serialized data before allocating","Ensure variable-width column offset tables end within the slice capacity","Cap aggregation/concatenation sizes well below MAX_ARRAY_SIZE"],"tags":["java","presto","slice-too-large","allocation"],"backgroundTag":"slice-too-large","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"}