diff --git a/api/maven-api-xml/src/main/java/org/apache/maven/api/xml/ImmutableCollections.java b/api/maven-api-xml/src/main/java/org/apache/maven/api/xml/ImmutableCollections.java index ca1c4707e890..243f888bc1e2 100644 --- a/api/maven-api-xml/src/main/java/org/apache/maven/api/xml/ImmutableCollections.java +++ b/api/maven-api-xml/src/main/java/org/apache/maven/api/xml/ImmutableCollections.java @@ -297,7 +297,7 @@ private SubList(int fromIndex, int toIndex) { @Override public E get(int index) { - if (index < 0 || index > size()) { + if (index < 0 || index >= size()) { throw outOfBounds(index); } return AbstractImmutableList.this.get(fromIndex + index); diff --git a/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java b/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java index dc6651da94d9..c16eb54637f5 100644 --- a/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java +++ b/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java @@ -297,7 +297,7 @@ private SubList(int fromIndex, int toIndex) { @Override public E get(int index) { - if (index < 0 || index > size()) { + if (index < 0 || index >= size()) { throw outOfBounds(index); } return AbstractImmutableList.this.get(fromIndex + index); diff --git a/impl/maven-xml/src/test/java/org/apache/maven/internal/xml/ImmutableCollectionsTest.java b/impl/maven-xml/src/test/java/org/apache/maven/internal/xml/ImmutableCollectionsTest.java new file mode 100644 index 000000000000..651714ba9ff2 --- /dev/null +++ b/impl/maven-xml/src/test/java/org/apache/maven/internal/xml/ImmutableCollectionsTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.internal.xml; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ImmutableCollectionsTest { + + private static List abc() { + return ImmutableCollections.copy(Arrays.asList("a", "b", "c")); + } + + @Test + void subListRejectsIndexEqualToItsSize() { + List sub = abc().subList(0, 1); + + assertEquals(1, sub.size()); + assertEquals("a", sub.get(0)); + assertThrows(IndexOutOfBoundsException.class, () -> sub.get(1)); + } + + @Test + void subListDoesNotReachPastItsEnd() { + // A sub list that stops before the end of the backing list is the case that used to + // read past its own bounds instead of failing: the parent list resolves the index + // and hands back an element the sub list does not contain. + List sub = abc().subList(1, 2); + + assertEquals(List.of("b"), sub); + assertThrows(IndexOutOfBoundsException.class, () -> sub.get(1)); + assertThrows(IndexOutOfBoundsException.class, () -> sub.get(-1)); + } + + @Test + void subListIterationStaysWithinBounds() { + List sub = abc().subList(0, 2); + + assertEquals(List.of("a", "b"), sub); + assertEquals(List.of("a", "b"), List.copyOf(sub)); + } + + @Test + void listIteratorStillAcceptsIndexEqualToSize() { + // List.listIterator(int) is specified to accept size() as a valid cursor position, + // so that bound is deliberately not the same as the one for get(int). + List list = abc(); + + assertEquals(3, list.size()); + assertEquals(false, list.listIterator(3).hasNext()); + assertThrows(IndexOutOfBoundsException.class, () -> list.listIterator(4)); + } +} diff --git a/src/mdo/java/ImmutableCollections.java b/src/mdo/java/ImmutableCollections.java index 4b69b4d3f554..0c0b32ca80be 100644 --- a/src/mdo/java/ImmutableCollections.java +++ b/src/mdo/java/ImmutableCollections.java @@ -300,7 +300,7 @@ private SubList(int fromIndex, int toIndex) { @Override public E get(int index) { - if (index < 0 || index > size()) { + if (index < 0 || index >= size()) { throw outOfBounds(index); } return AbstractImmutableList.this.get(fromIndex + index);