From 6f2b4ce9a9569e08916e140d8cd4e59fa08ce57f Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 2 Feb 2014 22:27:02 -0500 Subject: [PATCH] fixes #2341 - inconsistent bounds checks The core datastructure IntLost, FloatList, and StringList all have unsafe .get methods that do not perform bounds checking. This is in contrast to their .remove methods, which do perform bounds checking. Prior to this patch, the following would print 0: IntList il = new IntList(); println(il.get(5)); But if we tried to *remove* that element, we would get an ArrayIndexOutOfBoundException: il.remove(5); This patch causes calls to .get to throw exceptions instead of returning 0 (or null in the case of StringList) for uninitialized values. --- core/src/processing/data/FloatList.java | 3 +++ core/src/processing/data/IntList.java | 3 +++ core/src/processing/data/StringList.java | 3 +++ 3 files changed, 9 insertions(+) diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 9921cb326..b379bf0b2 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -110,6 +110,9 @@ public class FloatList implements Iterable { * @brief Get an entry at a particular index */ public float get(int index) { + if (index >= count) { + throw new ArrayIndexOutOfBoundsException(index); + } return data[index]; } diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 78775be3f..ea2d74af7 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -130,6 +130,9 @@ public class IntList implements Iterable { * @brief Get an entry at a particular index */ public int get(int index) { + if (index >= this.count) { + throw new ArrayIndexOutOfBoundsException(index); + } return data[index]; } diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index a407265a9..7811ee014 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -113,6 +113,9 @@ public class StringList implements Iterable { * @brief Get an entry at a particular index */ public String get(int index) { + if (index >= count) { + throw new ArrayIndexOutOfBoundsException(index); + } return data[index]; }