OSDN Git Service

Regex metacharacters result in error post search
authorRichard MacGregor <rmacgregor@cyngn.com>
Mon, 24 Aug 2015 22:26:07 +0000 (15:26 -0700)
committerStephen Bird <sbird@cyngn.com>
Wed, 26 Aug 2015 21:56:23 +0000 (14:56 -0700)
After receiving search results, the user query was used to sort and
highlight results. If a part of the search included an incomplete regex
syntax (such as a regex metacharacter) it fails to compile the pattern.

Repro steps:
1) Create file with regex metacharactes in name Example: "(test).txt"
2) Search for file using partial name, and only single regex character
(from a set such as "()" or "[]"). Example: "("

Expected results:
Search shows correct files

Observed results:
Search ends up crashing FileManager

Change-Id: Idf5ee3b441481574a5bada1ca7e9048109a13435
Ticket: QRDL-1035

src/com/cyanogenmod/filemanager/util/SearchHelper.java

index 6aa91ba..55ff5e8 100644 (file)
@@ -22,6 +22,7 @@ import android.text.SpannableString;
 import android.text.style.BackgroundColorSpan;
 import android.text.style.StyleSpan;
 
+import android.util.Log;
 import com.cyanogenmod.filemanager.model.FileSystemObject;
 import com.cyanogenmod.filemanager.model.Query;
 import com.cyanogenmod.filemanager.model.SearchResult;
@@ -31,13 +32,14 @@ import java.util.List;
 import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 
 /**
  * A helper class with useful methods for deal with search results.
  */
 public final class SearchHelper {
-
+    private static final String TAG = SearchHelper.class.getSimpleName();
     private static final String REGEXP_WILCARD = "*";  //$NON-NLS-1$
     private static final String REGEXP_WILCARD_JAVA = ".*";  //$NON-NLS-1$
 
@@ -131,7 +133,13 @@ public final class SearchHelper {
                     queries.get(i)
                         .replace(".", "[.]") //$NON-NLS-1$//$NON-NLS-2$
                         .replace("*", ".*"); //$NON-NLS-1$//$NON-NLS-2$
-            Pattern pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE);
+            Pattern pattern;
+            try {
+                pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE);
+            } catch (PatternSyntaxException e) {
+                Log.w(TAG, "Invalid regex syntax. Using literal query. Error=" + e);
+                pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE | Pattern.LITERAL);
+            }
             Matcher matcher = pattern.matcher(name);
             Spannable span =  new SpannableString(name);
             if (matcher.find()) {
@@ -230,7 +238,13 @@ public final class SearchHelper {
                     terms.get(i)
                         .replace(".", "[.]") //$NON-NLS-1$//$NON-NLS-2$
                         .replace("*", ".*"); //$NON-NLS-1$//$NON-NLS-2$
-            Pattern pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE);
+            Pattern pattern;
+            try {
+                pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE);
+            } catch (PatternSyntaxException e) {
+                Log.w(TAG, "Invalid regex syntax. Using literal query. Error=" + e);
+                pattern = Pattern.compile(query, Pattern.CASE_INSENSITIVE | Pattern.LITERAL);
+            }
             Matcher matcher = pattern.matcher(name);
             if (matcher.find()) {
                 //By name