From 17d453ea85703a5bc26c0669231be4d6f0d8bbac Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Tue, 5 Mar 2019 15:30:42 -0800 Subject: Checkng upper bound in *Array classes. *Array classes will now throw an IndexOutOfBoundsException if a caller tries to get or set a value for an index that's invalid based on the current size. Bug: 118339123 Test: atest CtsUtilTestCases Change-Id: Iddc9a0c7c89e0ac743b0380049527a1b2dfb434f --- core/java/android/util/SparseArray.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'core/java/android/util/SparseArray.java') diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index 89ea2d35fc2f..67dfb02a0b95 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -16,10 +16,11 @@ package android.util; +import android.annotation.UnsupportedAppUsage; + import com.android.internal.util.ArrayUtils; import com.android.internal.util.GrowingArrayUtils; -import android.annotation.UnsupportedAppUsage; import libcore.util.EmptyArray; /** @@ -171,6 +172,10 @@ public class SparseArray implements Cloneable { * the behavior is undefined.

*/ public void removeAt(int index) { + if (index >= mSize) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + throw new ArrayIndexOutOfBoundsException(index); + } if (mValues[index] != DELETED) { mValues[index] = DELETED; mGarbage = true; @@ -279,6 +284,10 @@ public class SparseArray implements Cloneable { * the behavior is undefined.

*/ public int keyAt(int index) { + if (index >= mSize) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } @@ -302,6 +311,10 @@ public class SparseArray implements Cloneable { */ @SuppressWarnings("unchecked") public E valueAt(int index) { + if (index >= mSize) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } @@ -317,6 +330,10 @@ public class SparseArray implements Cloneable { *

For indices outside of the range 0...size()-1, the behavior is undefined.

*/ public void setValueAt(int index, E value) { + if (index >= mSize) { + // The array might be slightly bigger than mSize, in which case, indexing won't fail. + throw new ArrayIndexOutOfBoundsException(index); + } if (mGarbage) { gc(); } -- cgit v1.2.3