From f2f673ba38d50fb224bf984f429f585eea74d605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= <51790620+jukzi@users.noreply.github.com> Date: Mon, 5 Sep 2022 12:20:15 +0200 Subject: [PATCH] fix StringIndexOutOfBoundsException during File Search #79 (#82) If same search is started in parallel. java.util.regex.Matcher is not thread-safe. --- .../search/ui/text/FileTextSearchScope.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/org.eclipse.search/new search/org/eclipse/search/ui/text/FileTextSearchScope.java b/org.eclipse.search/new search/org/eclipse/search/ui/text/FileTextSearchScope.java index 94affc8f0b4..afdaed46cc5 100644 --- a/org.eclipse.search/new search/org/eclipse/search/ui/text/FileTextSearchScope.java +++ b/org.eclipse.search/new search/org/eclipse/search/ui/text/FileTextSearchScope.java @@ -127,8 +127,8 @@ public static FileTextSearchScope newSearchScope(IWorkingSet[] workingSets, Stri private final String fDescription; private final IResource[] fRootElements; private final String[] fFileNamePatterns; - private final Matcher fPositiveFileNameMatcher; - private final Matcher fNegativeFileNameMatcher; + private final ThreadLocal fPositiveFileNameMatcher; + private final ThreadLocal fNegativeFileNameMatcher; private boolean fVisitDerived; private IWorkingSet[] fWorkingSets; @@ -139,8 +139,8 @@ private FileTextSearchScope(String description, IResource[] resources, IWorkingS fFileNamePatterns= fileNamePatterns; fVisitDerived= visitDerived; fWorkingSets= workingSets; - fPositiveFileNameMatcher= createMatcher(fileNamePatterns, false); - fNegativeFileNameMatcher= createMatcher(fileNamePatterns, true); + fPositiveFileNameMatcher = ThreadLocal.withInitial(() -> createMatcher(fileNamePatterns, false)); + fNegativeFileNameMatcher = ThreadLocal.withInitial(() -> createMatcher(fileNamePatterns, true)); } /** @@ -223,10 +223,12 @@ public boolean contains(IResourceProxy proxy) { } private boolean matchesFileName(String fileName) { - if (fPositiveFileNameMatcher != null && !fPositiveFileNameMatcher.reset(fileName).matches()) { + Matcher positiveFileNameMatcher = fPositiveFileNameMatcher.get(); + if (positiveFileNameMatcher != null && !positiveFileNameMatcher.reset(fileName).matches()) { return false; } - if (fNegativeFileNameMatcher != null && fNegativeFileNameMatcher.reset(fileName).matches()) { + Matcher negativeFileNameMatcher = fNegativeFileNameMatcher.get(); + if (negativeFileNameMatcher != null && negativeFileNameMatcher.reset(fileName).matches()) { return false; } return true;