Date: Thu, 25 Apr 2019 16:16:34 -0700
Subject: Gating OutOfBoundsException on targetSdkVersion.
Apps targeting Pie or older will get the old undefined behavior. Apps
targeting Q or newer will get the OutOfBoundsException.
Bug: 118339123
Test: atest CtsUtilTestCases
Change-Id: Ibf5467aadec4a2f76ee180e963afeaf5a8a013a2
---
core/java/android/util/SparseIntArray.java | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
(limited to 'core/java/android/util/SparseIntArray.java')
diff --git a/core/java/android/util/SparseIntArray.java b/core/java/android/util/SparseIntArray.java
index c68dc4edcfb7..84f92690b3cf 100644
--- a/core/java/android/util/SparseIntArray.java
+++ b/core/java/android/util/SparseIntArray.java
@@ -170,9 +170,14 @@ public class SparseIntArray implements Cloneable {
* be in ascending order, e.g., keyAt(0) will return the
* smallest key and keyAt(size()-1) will return the largest
* key.
+ *
+ * For indices outside of the range 0...size()-1, the behavior is undefined for
+ * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an
+ * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting
+ * {@link android.os.Build.VERSION_CODES#Q} and later.
*/
public int keyAt(int index) {
- if (index >= mSize) {
+ if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
throw new ArrayIndexOutOfBoundsException(index);
}
@@ -189,9 +194,14 @@ public class SparseIntArray implements Cloneable {
* valueAt(0) will return the value associated with the
* smallest key and valueAt(size()-1) will return the value
* associated with the largest key.
+ *
+ * For indices outside of the range 0...size()-1, the behavior is undefined for
+ * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an
+ * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting
+ * {@link android.os.Build.VERSION_CODES#Q} and later.
*/
public int valueAt(int index) {
- if (index >= mSize) {
+ if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
throw new ArrayIndexOutOfBoundsException(index);
}
@@ -200,9 +210,14 @@ public class SparseIntArray implements Cloneable {
/**
* Directly set the value at a particular index.
+ *
+ * For indices outside of the range 0...size()-1, the behavior is undefined for
+ * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an
+ * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting
+ * {@link android.os.Build.VERSION_CODES#Q} and later.
*/
public void setValueAt(int index, int value) {
- if (index >= mSize) {
+ if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
throw new ArrayIndexOutOfBoundsException(index);
}
--
cgit v1.2.3
From 3858b2d1dcd9a5105f6a895b25d215a1d31ae5bc Mon Sep 17 00:00:00 2001
From: Kweku Adams
Date: Mon, 29 Apr 2019 11:47:41 -0700
Subject: Add extra comment for implementation.
Add a comment noting that the check to throw the exception is
intentionally second so that it's out of the critical path.
Bug: 118339123
Test: N/A
Change-Id: I36c5ea67579bcd7906f711530392110d9987ffb4
---
core/java/android/util/SparseIntArray.java | 3 +++
1 file changed, 3 insertions(+)
(limited to 'core/java/android/util/SparseIntArray.java')
diff --git a/core/java/android/util/SparseIntArray.java b/core/java/android/util/SparseIntArray.java
index 84f92690b3cf..1ca1717828fa 100644
--- a/core/java/android/util/SparseIntArray.java
+++ b/core/java/android/util/SparseIntArray.java
@@ -179,6 +179,7 @@ public class SparseIntArray implements Cloneable {
public int keyAt(int index) {
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ // Check if exception should be thrown outside of the critical path.
throw new ArrayIndexOutOfBoundsException(index);
}
return mKeys[index];
@@ -203,6 +204,7 @@ public class SparseIntArray implements Cloneable {
public int valueAt(int index) {
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ // Check if exception should be thrown outside of the critical path.
throw new ArrayIndexOutOfBoundsException(index);
}
return mValues[index];
@@ -219,6 +221,7 @@ public class SparseIntArray implements Cloneable {
public void setValueAt(int index, int value) {
if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) {
// The array might be slightly bigger than mSize, in which case, indexing won't fail.
+ // Check if exception should be thrown outside of the critical path.
throw new ArrayIndexOutOfBoundsException(index);
}
mValues[index] = value;
--
cgit v1.2.3