This is an automated email from the ASF dual-hosted git repository.
ggregory 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 99c8a4d11 Move argument check deeper in.
99c8a4d11 is described below
commit 99c8a4d11823b554ea9a28015d224ae8bdd4224a
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jan 15 12:31:44 2026 -0500
Move argument check deeper in.
---
.../apache/commons/collections4/list/TreeList.java | 25 ++++++++++++++++------
.../commons/collections4/list/TreeListTest.java | 12 +++++++++++
2 files changed, 31 insertions(+), 6 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 a0e1c7145..57c15b24e 100644
--- a/src/main/java/org/apache/commons/collections4/list/TreeList.java
+++ b/src/main/java/org/apache/commons/collections4/list/TreeList.java
@@ -804,6 +804,7 @@ public class TreeList<E> extends AbstractList<E> {
* @param fromIndex the index to start at
*/
protected TreeListIterator(final TreeList<E> parent, final int
fromIndex) {
+ checkInterval(fromIndex, 0, parent.size(), parent.size());
this.parent = parent;
this.expectedModCount = parent.modCount;
this.next = parent.root == null ? null :
parent.root.get(fromIndex);
@@ -916,6 +917,21 @@ public class TreeList<E> extends AbstractList<E> {
}
}
+ /**
+ * Checks whether the index is valid.
+ *
+ * @param index the index to check.
+ * @param startIndex the first allowed index.
+ * @param endIndex the last allowed index.
+ * @param endIndex the size.
+ * @throws IndexOutOfBoundsException if the index is invalid
+ */
+ private static void checkInterval(final int index, final int startIndex,
final int endIndex, final int size) {
+ if (index < startIndex || index > endIndex) {
+ throw new IndexOutOfBoundsException("Invalid index:" + index + ",
size=" + size);
+ }
+ }
+
/** The root node in the AVL tree */
private AVLNode<E> root;
@@ -994,9 +1010,7 @@ public class TreeList<E> extends AbstractList<E> {
* @throws IndexOutOfBoundsException if the index is invalid
*/
private void checkInterval(final int index, final int startIndex, final
int endIndex) {
- if (index < startIndex || index > endIndex) {
- throw new IndexOutOfBoundsException("Invalid index:" + index + ",
size=" + size());
- }
+ checkInterval(index, startIndex, endIndex, size());
}
/**
@@ -1072,14 +1086,13 @@ public class TreeList<E> extends AbstractList<E> {
/**
* Gets a ListIterator over the list.
*
- * @param fromIndex the index to start from
- * @return the new iterator
+ * @param fromIndex the index to start from.
+ * @return the new iterator.
*/
@Override
public ListIterator<E> listIterator(final int fromIndex) {
// override to go 75% faster
// cannot use EmptyIterator as iterator.add() must work
- checkInterval(fromIndex, 0, size());
return new TreeListIterator<>(this, fromIndex);
}
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 ec5b683e6..1f502208e 100644
--- a/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
+++ b/src/test/java/org/apache/commons/collections4/list/TreeListTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.collections4.list;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.List;
@@ -100,6 +101,17 @@ public class TreeListTest<E> extends AbstractListTest<E> {
return new TreeList<>();
}
+ @Test
+ void testPreviousNullPointerException() throws Throwable {
+ final TreeList<String> treeList = new TreeList<>();
+ treeList.add("a");
+ treeList.add("b");
+ assertThrows(IndexOutOfBoundsException.class, () ->
treeList.listIterator(3).previous());
+ assertThrows(IndexOutOfBoundsException.class, () -> new
TreeList.TreeListIterator<String>(treeList, 3));
+ // Test doesn't get to previous()
+ assertThrows(IndexOutOfBoundsException.class, () -> new
TreeList.TreeListIterator<String>(treeList, 3).previous());
+ }
+
@Test
@SuppressWarnings("unchecked")
void testAddMultiple() {