OSDN Git Service

Fix android.text.cts.TextUtilsTest#testRegionMatches
authorRaph Levien <raph@google.com>
Wed, 14 May 2014 22:46:47 +0000 (15:46 -0700)
committerRaph Levien <raph@google.com>
Wed, 14 May 2014 22:46:47 +0000 (15:46 -0700)
The CTS test expects an ArrayIndexOutOfBounds exception when passing in
an unreasonably large value for len. Since the actual implementation was
causing an integer overflow, we were getting a different exception.
Since integer overflow is potentially dangerous, this patch tests for it
and throws an exception explicitly.

Change-Id: I0420c06185d33d130853861d25d4f65b06fe0dfa

core/java/android/text/TextUtils.java

index f06ae71..48122d6 100644 (file)
@@ -48,10 +48,11 @@ import android.text.style.URLSpan;
 import android.text.style.UnderlineSpan;
 import android.util.Log;
 import android.util.Printer;
-
 import android.view.View;
+
 import com.android.internal.R;
 import com.android.internal.util.ArrayUtils;
+
 import libcore.icu.ICU;
 
 import java.lang.reflect.Array;
@@ -229,7 +230,12 @@ public class TextUtils {
     public static boolean regionMatches(CharSequence one, int toffset,
                                         CharSequence two, int ooffset,
                                         int len) {
-        char[] temp = obtain(2 * len);
+        int tempLen = 2 * len;
+        if (tempLen < len) {
+            // Integer overflow; len is unreasonably large
+            throw new IndexOutOfBoundsException();
+        }
+        char[] temp = obtain(tempLen);
 
         getChars(one, toffset, toffset + len, temp, 0);
         getChars(two, ooffset, ooffset + len, temp, len);