Backport COLLECTIONS-447 to 3.2.2.

git-svn-id: 
https://svn.apache.org/repos/asf/commons/proper/collections/branches/COLLECTIONS_3_2_X@1713536
 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/commons-collections/commit/fd61086d
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/fd61086d
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/fd61086d

Branch: refs/heads/COLLECTIONS_3_2_X
Commit: fd61086df2f169190db670553bfee2ebdc140533
Parents: 7403718
Author: Thomas Neidhart <[email protected]>
Authored: Mon Nov 9 20:53:04 2015 +0000
Committer: Thomas Neidhart <[email protected]>
Committed: Mon Nov 9 20:53:04 2015 +0000

----------------------------------------------------------------------
 src/changes/changes.xml                              | 13 ++++---------
 .../apache/commons/collections/list/TreeList.java    | 11 +++++------
 .../commons/collections/list/TestTreeList.java       | 15 +++++++++++++++
 3 files changed, 24 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-collections/blob/fd61086d/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6656de6..83a0427 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,7 +22,6 @@
   <body>
 
   <release version="3.2.2" date="20XX-XX-XX" description="This is a bugfix 
release.">
-
     <action issue="COLLECTIONS-580" dev="tn" type="update">
       De-serialization of "InvokerTransformer" is disabled by default as this
       can be exploited for remote code execution attacks. To re-enable the
@@ -35,6 +34,10 @@
       permission to read system properties, the "File#separator" field will
       be used instead.
     </action>
+    <action issue="COLLECTIONS-447" dev="tn" type="fix" due-to="Jeffrey 
Barnes">
+      Tree traversal with a TreeListIterator will not be affected anymore by
+      the removal of an element directly after a call to previous().
+    </action>
     <action issue="COLLECTIONS-444" dev="tn" type="fix" due-to="Thomas Vahrst, 
John Vasileff">
       SetUniqueList.set(int, Object) now works correctly if the object to be 
inserted
       is already placed at the given position.
@@ -81,14 +84,6 @@
       Calling "setValue(Object)" on any Entry returned by a "Flat3Map" will now
       correctly set the value for the current entry.
     </action>
-  
-  <!--  Bugfixes planned to backport from 4.0
-  
-    <action issue="COLLECTIONS-447" dev="tn" type="fix" due-to="Jeffrey 
Barnes">
-      Tree traversal with a TreeListIterator will not be affected anymore by
-      the removal of an element directly after a call to previous().
-    </action>
-    -->
   </release>
   </body>
 </document>

http://git-wip-us.apache.org/repos/asf/commons-collections/blob/fd61086d/src/java/org/apache/commons/collections/list/TreeList.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/collections/list/TreeList.java 
b/src/java/org/apache/commons/collections/list/TreeList.java
index 54816ac..39b077c 100644
--- a/src/java/org/apache/commons/collections/list/TreeList.java
+++ b/src/java/org/apache/commons/collections/list/TreeList.java
@@ -881,15 +881,14 @@ public class TreeList extends AbstractList {
             if (currentIndex == -1) {
                 throw new IllegalStateException();
             }
-            if (nextIndex == currentIndex) {
-                // remove() following previous()
-                next = next.next();
-                parent.remove(currentIndex);
-            } else {
+            parent.remove(currentIndex);
+            if (nextIndex != currentIndex) {
                 // remove() following next()
-                parent.remove(currentIndex);
                 nextIndex--;
             }
+            // the AVL node referenced by next may have become stale after a 
remove
+            // reset it now: will be retrieved by next call to 
next()/previous() via nextIndex
+            next = null;
             current = null;
             currentIndex = -1;
             expectedModCount++;

http://git-wip-us.apache.org/repos/asf/commons-collections/blob/fd61086d/src/test/org/apache/commons/collections/list/TestTreeList.java
----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/collections/list/TestTreeList.java 
b/src/test/org/apache/commons/collections/list/TestTreeList.java
index d3ae8b9..b78b0f5 100644
--- a/src/test/org/apache/commons/collections/list/TestTreeList.java
+++ b/src/test/org/apache/commons/collections/list/TestTreeList.java
@@ -242,4 +242,19 @@ public class TestTreeList extends AbstractTestList {
         assertEquals(false, li.hasNext());
     }
 
+    public void testBugCollections447() {
+        final List treeList = new TreeList();
+        treeList.add("A");
+        treeList.add("B");
+        treeList.add("C");
+        treeList.add("D");
+        final ListIterator li = treeList.listIterator();
+        assertEquals("A", li.next());
+        assertEquals("B", li.next());
+        assertEquals("B", li.previous());
+        li.remove(); // Deletes "B"
+        // previous() after remove() should move to
+        // the element before the one just removed
+        assertEquals("A", li.previous());
+    }    
 }

Reply via email to