Markus Sunela created NETBEANS-2982: ---------------------------------------
Summary: UndoRedo class causes ArrayIndexOutOfBoundsException Key: NETBEANS-2982 URL: https://issues.apache.org/jira/browse/NETBEANS-2982 Project: NetBeans Issue Type: Bug Components: platform - Window System Affects Versions: 11.1, 11.0, 10.0 Reporter: Markus Sunela After a lot of UndoableEdits have been added to an UndoManager in a platform application, the UI sometimes starts to show the exception as it tries to update the name of the next undoable edit for the menus: {code:java} java.lang.ArrayIndexOutOfBoundsException: 100 >= 100 at java.base/java.util.Vector.elementAt(Vector.java:496) at org.openide.awt.UndoRedo$Manager.editToBeUndone(UndoRedo.java:403) at org.openide.awt.UndoRedo$Manager.canUndo(UndoRedo.java:224) at org.openide.awt.UndoRedo$Manager.getUndoPresentationName(UndoRedo.java:501) at org.openide.actions.UndoRedoAction.getName(UndoRedoAction.java:171) at org.openide.actions.UndoRedoAction.run(UndoRedoAction.java:139) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740) at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:136) {code} This seems to be due to incorrect handling of indexOfNextAdd in the UndoRedo.java ([https://github.com/apache/netbeans/blob/master/platform/openide.awt/src/org/openide/awt/UndoRedo.java)]. The indexOfNextAdd can be larger (more than by one) than the length of the edits Vector, and especially in editToBeUndone the edits Vector is directly accessed at indexOfNextAdd without any bounds checking: {code:java} @Override protected UndoableEdtit editToBeUndone() { int i = indexOfNextAdd; while (i > 0) { UndoableEdit edit = edits.elementAt(--i); if (edit.isSignificant()) { return edit; } } return null; } {code} For example editToBeRedone returns null in the case indexOfNextAdd >= edit.size(). I assume the root cause to be the trimEdits method incorrectly updating the indexOfNextAdd, possibly due to a negative value being passed from trimToLimits: {code:java} if (keepFrom < 0) { keepTo -= keepFrom; keepFrom = 0; } if (keepTo >= size) { int delta = size - keepTo - 1; keepTo += delta; keepFrom += delta; } {code} At least, the latter if doesn't ensure non-negative values for either keepTo or keepFrom, which could cause the bug. -- This message was sent by Atlassian JIRA (v7.6.14#76016) --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists