diff options
author | Richard MacGregor <rmacgregor@cyngn.com> | 2015-08-24 15:26:07 -0700 |
---|---|---|
committer | Stephen Bird <sbird@cyngn.com> | 2015-08-26 14:56:23 -0700 |
commit | 1ad09f7b3212f49e478f7d2f2b1cb7674ac6feb0 (patch) | |
tree | f251df996c8a72ad567bc01c8c02ff5e5a3b1f3e | |
parent | 1a23b92dc1d3a1a2f9b386139c40281d9c24fc26 (diff) | |
download | android_packages_apps_CMFileManager-1ad09f7b3212f49e478f7d2f2b1cb7674ac6feb0.tar.gz android_packages_apps_CMFileManager-1ad09f7b3212f49e478f7d2f2b1cb7674ac6feb0.tar.bz2 android_packages_apps_CMFileManager-1ad09f7b3212f49e478f7d2f2b1cb7674ac6feb0.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 |