diff options
| author | Richard MacGregor <rmacgregor@cyngn.com> | 2015-08-24 15:26:07 -0700 |
|---|---|---|
| committer | Richard MacGregor <rmacgregor@cyngn.com> | 2015-08-26 16:56:46 -0700 |
| commit | 207f87e1c6a278cc0adbf7a295c17a4c4e4a45e9 (patch) | |
| tree | 8f6df50c0abde9133efcee469da811cde4c940ec | |
| parent | 421307c1d080f40c67649f1df507d0e6e6b622c9 (diff) | |
| download | android_packages_apps_CMFileManager-207f87e1c6a278cc0adbf7a295c17a4c4e4a45e9.tar.gz android_packages_apps_CMFileManager-207f87e1c6a278cc0adbf7a295c17a4c4e4a45e9.tar.bz2 android_packages_apps_CMFileManager-207f87e1c6a278cc0adbf7a295c17a4c4e4a45e9.zip | |
Regex metacharacters result in error post search
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
| -rw-r--r-- | src/com/cyanogenmod/filemanager/util/SearchHelper.java | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/com/cyanogenmod/filemanager/util/SearchHelper.java b/src/com/cyanogenmod/filemanager/util/SearchHelper.java index 6aa91ba9..55ff5e8f 100644 --- a/src/com/cyanogenmod/filemanager/util/SearchHelper.java +++ b/src/com/cyanogenmod/filemanager/util/SearchHelper.java @@ -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 |
