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/SparseLongArray.java | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'core/java/android/util/SparseLongArray.java') diff --git a/core/java/android/util/SparseLongArray.java b/core/java/android/util/SparseLongArray.java index 81db2b7ff715..37a92024f374 100644 --- a/core/java/android/util/SparseLongArray.java +++ b/core/java/android/util/SparseLongArray.java @@ -182,6 +182,10 @@ public class SparseLongArray implements Cloneable { * key.

*/ 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); + } return mKeys[index]; } @@ -197,6 +201,10 @@ public class SparseLongArray implements Cloneable { * associated with the largest key.

*/ public long 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); + } return mValues[index]; } -- cgit v1.2.3