{"record":{"id":"d23d7f00cc9c0524","repo":"oracle/graal","slug":"toindex-toindex","errorCode":null,"errorMessage":"toIndex = ${toIndex}","messagePattern":"toIndex = (.+?)","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"espresso/src/com.oracle.truffle.espresso.polyglot/src/com/oracle/truffle/espresso/polyglot/collections/AbstractListWithoutField.java","lineNumber":442,"sourceCode":"     *           backing list. The {@code iterator} method merely returns {@code listIterator()},\n     *           and the {@code size} method merely returns the subclass's {@code size} field.\n     *\n     * @throws IndexOutOfBoundsException if an endpoint index value is out of range\n     *             {@code (fromIndex < 0 || toIndex > size)}\n     * @throws IllegalArgumentException if the endpoint indices are out of order\n     *             {@code (fromIndex > toIndex)}\n     */\n    public List<E> subList(int fromIndex, int toIndex) {\n        subListRangeCheck(fromIndex, toIndex, size());\n        return (this instanceof RandomAccess ? new RandomAccessSubList<>(this, fromIndex, toIndex) : new SubList<>(this, fromIndex, toIndex));\n    }\n\n    static void subListRangeCheck(int fromIndex, int toIndex, int size) {\n        if (fromIndex < 0) {\n            throw new IndexOutOfBoundsException(\"fromIndex = \" + fromIndex);\n        }\n        if (toIndex > size) {\n            throw new IndexOutOfBoundsException(\"toIndex = \" + toIndex);\n        }\n        if (fromIndex > toIndex) {\n            throw new IllegalArgumentException(\"fromIndex(\" + fromIndex +\n                            \") > toIndex(\" + toIndex + \")\");\n        }\n    }\n\n    // Comparison and hashing\n\n    /**\n     * Compares the specified object with this list for equality. Returns {@code true} if and only\n     * if the specified object is also a list, both lists have the same size, and all corresponding\n     * pairs of elements in the two lists are <i>equal</i>. (Two elements {@code e1} and {@code e2}\n     * are <i>equal</i> if {@code (e1==null ? e2==null :\n     * e1.equals(e2))}.) In other words, two lists are defined to be equal if they contain the same\n     * elements in the same order.\n     *\n     * @implSpec This implementation first checks if the specified object is this list. If so, it","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso/src/com.oracle.truffle.espresso.polyglot/src/com/oracle/truffle/espresso/polyglot/collections/AbstractListWithoutField.java#L424-L460","documentation":"Thrown by AbstractListWithoutField.subList range checks when toIndex exceeds the list's size. The sublist view must stay inside [0, size]; an end index past the last element is rejected immediately.","triggerScenarios":"Calling subList(from, to) with to > size(), e.g. subList(0, 10) on a 5-element list; computing an end offset as start + count without clamping; calling subList on a list that shrank between the size check and the call (concurrent modification).","commonSituations":"Paging logic 'subList(page * size, (page + 1) * size)' on the last, partial page; slicing a list after another thread removed elements; copying java.util.List sample code onto Espresso collections with different sizes.","solutions":["Clamp the end index: subList(start, Math.min(end, list.size())).","Re-read size() at the moment of the call rather than caching it.","For last-page paging compute int end = Math.min(list.size(), start + pageSize).","Guard with if (from > list.size()) return empty before slicing."],"exampleFix":"// before\nList<E> page = list.subList(start, start + pageSize); // fails on last page\n\n// after\nint end = Math.min(start + pageSize, list.size());\nList<E> page = list.subList(start, end);","handlingStrategy":"validation","validationCode":"int end = Math.min(requestedEnd, list.size());\nif (fromIndex < 0 || fromIndex > end) {\n    throw new IllegalArgumentException(\"bad range: \" + fromIndex + \"..\" + end);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always clamp the end index to size() at call time.","Recompute size() for mutable lists instead of caching.","Use min(start + pageSize, size) for pagination."],"tags":["collections","index-out-of-bounds","sublist","espresso","polyglot"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}