Skip to content

Commit 94c5837

Browse files
committed
ListIterator: listIterator() shall check the index bounds
1 parent 67ce028 commit 94c5837

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

objectbox-java/src/main/java/io/objectbox/query/LazyList.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ public ListIterator<E> listIterator() {
278278

279279
@Override
280280
public ListIterator<E> listIterator(int location) {
281+
if (location < 0 || location > size) {
282+
throw new IndexOutOfBoundsException("Index: " + location + ", Size: " + size);
283+
}
281284
return new LazyIterator(location);
282285
}
283286

tests/objectbox-java-test/src/test/java/io/objectbox/query/LazyListTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ public void testUncached() {
176176
}
177177
}
178178

179+
@Test
180+
public void listIterator_indexOutOfBounds_throws() {
181+
putTestEntities(3);
182+
LazyList<TestEntity> listLazy = getTestEntityBox().query().build().findLazyCached();
183+
int size = listLazy.size();
184+
185+
try {
186+
listLazy.listIterator(-1);
187+
fail("listIterator should throw for negative index");
188+
} catch (IndexOutOfBoundsException expected) {
189+
// Expected, OK
190+
}
191+
192+
try {
193+
listLazy.listIterator(size + 1);
194+
fail("listIterator should throw for index larger than size");
195+
} catch (IndexOutOfBoundsException expected) {
196+
// Expected, OK
197+
}
198+
199+
assertNotNull(listLazy.listIterator(size));
200+
}
201+
179202

180203
protected void assertIds(List<TestEntity> list, List<TestEntity> list2) {
181204
for (int i = 0; i < list.size(); i++) {

0 commit comments

Comments
 (0)