{"record":{"id":"31ad2cbedb6f6c75","repo":"oracle/graal","slug":"index-index-size-size","errorCode":null,"errorMessage":"Index: ${index}, Size: ${size}","messagePattern":"Index: (.+?), Size: (.+?)","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":539,"sourceCode":"     * @implSpec This implementation gets a list iterator positioned before {@code fromIndex}, and\n     *           repeatedly calls {@code ListIterator.next} followed by {@code ListIterator.remove}\n     *           until the entire range has been removed. <b>Note: if {@code ListIterator.remove}\n     *           requires linear time, this implementation requires quadratic time.</b>\n     *\n     * @param fromIndex index of first element to be removed\n     * @param toIndex index after last element to be removed\n     */\n    protected void removeRange(int fromIndex, int toIndex) {\n        ListIterator<E> it = listIterator(fromIndex);\n        for (int i = 0, n = toIndex - fromIndex; i < n; i++) {\n            it.next();\n            it.remove();\n        }\n    }\n\n    private void rangeCheckForAdd(int index) {\n        if (index < 0 || index > size()) {\n            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));\n        }\n    }\n\n    private String outOfBoundsMsg(int index) {\n        return \"Index: \" + index + \", Size: \" + size();\n    }\n\n    private static class SubList<E> extends AbstractListWithoutField<E> {\n        private final AbstractListWithoutField<E> root;\n        private final SubList<E> parent;\n        private final int offset;\n        protected int size;\n\n        /**\n         * Constructs a sublist of an arbitrary AbstractList, which is not a SubList itself.\n         */\n        SubList(AbstractListWithoutField<E> root, int fromIndex, int toIndex) {\n            this.root = root;","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso/src/com.oracle.truffle.espresso.polyglot/src/com/oracle/truffle/espresso/polyglot/collections/AbstractListWithoutField.java#L521-L557","documentation":"IndexOutOfBoundsException with message 'Index: N, Size: S', thrown by AbstractListWithoutField.rangeCheckForAdd when an insertion index is negative or greater than the list size. Valid insertion points for add are 0..size inclusive; anything else is rejected.","triggerScenarios":"Calling listIterator(index) or add(index, element) on the outer AbstractListWithoutField with index < 0 or index > size(); using an index returned by a failed search (-1); iterating with a cursor that ran past the end.","commonSituations":"Inserting at indexOf result without a found-check; concurrent shrink of the list between size() and add(); passing a cursor from a different list instance.","solutions":["Check 0 <= index <= list.size() before the add/iterator call.","Treat -1 from indexOf/lastIndexOf as 'not found' and skip the insert.","Recompute size immediately before inserting when other threads mutate the list.","Catch IndexOutOfBoundsException at user-input boundaries and report a validation error."],"exampleFix":"// before\nlist.add(position, element); // position may be -1 from a failed search\n\n// after\nint position = list.indexOf(key);\nif (position >= 0) {\n    list.add(position, element);\n}","handlingStrategy":"validation","validationCode":"if (index < 0 || index > list.size()) {\n    throw new IndexOutOfBoundsException(\"index \" + index + \" not in [0, \" + list.size() + \"]\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    list.add(index, element);\n} catch (IndexOutOfBoundsException e) {\n    // report validation error to caller; do not silently retry with stale index\n}","preventionTips":["Check indexOf results for -1 before using as an index.","Recheck size immediately before insertion on shared lists.","Validate user-supplied indices at the API boundary."],"tags":["collections","index-out-of-bounds","insert","espresso","polyglot"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}