Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 207f87e1 authored by Richard MacGregor's avatar Richard MacGregor
Browse files

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
parent 421307c1
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -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