Bug 1240500 - Early return on empty pattern to avoid an infinte loop and OOM r=rnewman draft
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 25 Feb 2016 15:28:08 -0800
changeset 334727 66dd11d9079cebb5b798eef86361d43b1f955e98
parent 334724 315cb89a66c74674b47da9e5e02fed7e53ce440a
child 514982 43325b6acaf9be8c852640a093a631ff7f54f3c4
push id11623
push userahunt@mozilla.com
push dateThu, 25 Feb 2016 23:28:47 +0000
reviewersrnewman
bugs1240500
milestone47.0a1
Bug 1240500 - Early return on empty pattern to avoid an infinte loop and OOM r=rnewman MozReview-Commit-ID: 7GlsZjfmE0y
mobile/android/base/java/org/mozilla/gecko/home/SearchEngineRow.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/SearchEngineRow.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/SearchEngineRow.java
@@ -20,16 +20,17 @@ import org.mozilla.gecko.widget.Animated
 import org.mozilla.gecko.widget.FaviconView;
 import org.mozilla.gecko.widget.FlowLayout;
 
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.graphics.drawable.Drawable;
 import android.graphics.Typeface;
 import android.support.annotation.Nullable;
+import android.text.TextUtils;
 import android.text.style.StyleSpan;
 import android.text.Spannable;
 import android.text.SpannableStringBuilder;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.animation.AlphaAnimation;
@@ -165,17 +166,25 @@ class SearchEngineRow extends AnimatedHe
      * Finds all occurrences of pattern in string and returns a list of the starting indices
      * of each occurrence.
      *
      * @param pattern The pattern that is searched for
      * @param string The string where we search for the pattern
      */
     private void refreshOccurrencesWith(String pattern, String string) {
         mOccurrences.clear();
+
+        // Don't try to search for an empty string - String.indexOf will return 0, which would result
+        // in us iterating with lastIndexOfMatch = 0, which eventually results in an OOM.
+        if (TextUtils.isEmpty(pattern)) {
+            return;
+        }
+
         final int patternLength = pattern.length();
+
         int indexOfMatch = 0;
         int lastIndexOfMatch = 0;
         while(indexOfMatch != -1) {
             indexOfMatch = string.indexOf(pattern, lastIndexOfMatch);
             lastIndexOfMatch = indexOfMatch + patternLength;
             if(indexOfMatch != -1) {
                 mOccurrences.add(indexOfMatch);
             }