Bug 1240500 - Early return on empty pattern to avoid an infinte loop and OOM r=rnewman
MozReview-Commit-ID: 7GlsZjfmE0y
--- 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);
}