Bug 1240500 - Early return on empty pattern to avoid an infinte loop and OOM. r=rnewman, a=lizzard
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 25 Feb 2016 15:28:08 -0800
changeset 317388 9e0d5e813a549f2b3bb67cfb9b3544711fc4e2b9
parent 317387 352f811d9d688898c9076b1d2d5fc20b85e58309
child 317389 828929bf8ea286f6323a18a7dd8b0b67c9a8bafc
push id5717
push usercbook@mozilla.com
push dateThu, 10 Mar 2016 08:57:56 +0000
treeherdermozilla-beta@9e0d5e813a54 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, lizzard
bugs1240500
milestone46.0
Bug 1240500 - Early return on empty pattern to avoid an infinte loop and OOM. r=rnewman, a=lizzard 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;
@@ -161,17 +162,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 List<Integer> findAllOccurrencesOf(String pattern, String string) {
         List<Integer> occurrences = new ArrayList<>();
+
+        // 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 occurrences;
+        }
+
         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) {
                 occurrences.add(indexOfMatch);
             }