Skip to content

Commit 2ed09ee

Browse files
committed
IntList get lower bounds check now enforced, test added
1 parent 4206874 commit 2ed09ee

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

core/src/processing/data/IntList.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Iterator;
77
import java.util.Random;
88

9+
import org.jetbrains.annotations.TestOnly;
910
import processing.core.PApplet;
1011

1112

@@ -164,13 +165,14 @@ public void clear() {
164165
* @webBrief Get an entry at a particular index
165166
*/
166167
public int get(int index) {
167-
if (index >= this.count) {
168+
if (index >= this.count || index < 0) {
168169
throw new ArrayIndexOutOfBoundsException(index);
169170
}
170171
return data[index];
171172
}
172173

173174

175+
174176
/**
175177
* Set the entry at a particular index.
176178
*

core/test/processing/data/IntListTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ public void testPopOnEmptyIntListThrowsException() {
151151
assertEquals("Can't call pop() on an empty list", exception.getMessage());
152152
}
153153

154+
@Test
155+
public void testGetOnNegativeIndexIntListThrowsException() {
156+
IntList testedList = new IntList();
157+
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
158+
testedList.get(-1);
159+
});
160+
161+
assertEquals("Array index out of range: -1", exception.getMessage());
162+
}
163+
154164
@Test
155165
public void testRemoveWithIndexGreaterThanSize() {
156166
IntList testedList = new IntList();

0 commit comments

Comments
 (0)