This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push:
new fe8eb4989 Increment modCount after the bounds check in TreeList add
and remove (#716)
fe8eb4989 is described below
commit fe8eb4989392cb436438978b151aa800b70195b9
Author: Naveed Khan <[email protected]>
AuthorDate: Wed Jul 22 14:05:17 2026 +0000
Increment modCount after the bounds check in TreeList add and remove (#716)
a rejected index left modCount advanced, invalidating a live iterator with
a spurious ConcurrentModificationException.
---
.../apache/commons/collections4/list/TreeList.java | 4 ++--
.../commons/collections4/list/TreeListTest.java | 19 +++++++++++++++++++
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/collections4/list/TreeList.java
b/src/main/java/org/apache/commons/collections4/list/TreeList.java
index d2e75eba4..68931b2ca 100644
--- a/src/main/java/org/apache/commons/collections4/list/TreeList.java
+++ b/src/main/java/org/apache/commons/collections4/list/TreeList.java
@@ -966,8 +966,8 @@ public class TreeList<E> extends AbstractList<E> {
*/
@Override
public void add(final int index, final E obj) {
- modCount++;
checkInterval(index, 0, size());
+ modCount++;
if (root == null) {
root = new AVLNode<>(index, obj, null, null);
} else {
@@ -1105,8 +1105,8 @@ public class TreeList<E> extends AbstractList<E> {
*/
@Override
public E remove(final int index) {
- modCount++;
checkInterval(index, 0, size() - 1);
+ modCount++;
final E result = get(index);
root = root.remove(index);
size--;
diff --git
a/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
b/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
index bff75d2bc..2f2adf05c 100644
--- a/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
+++ b/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
@@ -171,6 +171,25 @@ public class TreeListTest<E> extends AbstractListTest<E> {
assertEquals("A", li.previous());
}
+ @Test
+ void testFailedIndexedChangeKeepsIteratorValid() {
+ final List<String> list = new TreeList<>();
+ list.add("a");
+ list.add("b");
+
+ // a rejected add(index) is a no-op and must not invalidate a live
iterator
+ final ListIterator<String> addIt = list.listIterator();
+ assertThrows(IndexOutOfBoundsException.class, () -> list.add(5, "x"));
+ assertEquals(2, list.size());
+ assertEquals("a", addIt.next());
+
+ // same for a rejected remove(index)
+ final ListIterator<String> removeIt = list.listIterator();
+ assertThrows(IndexOutOfBoundsException.class, () -> list.remove(10));
+ assertEquals(2, list.size());
+ assertEquals("a", removeIt.next());
+ }
+
@Test
@SuppressWarnings("unchecked")
void testIndexOf() {