Skip to content

Commit

Permalink
fix StringIndexOutOfBoundsException during File Search eclipse-platfo…
Browse files Browse the repository at this point in the history
…rm#79 (eclipse-platform#82)

If same search is started in parallel.
java.util.regex.Matcher is not thread-safe.
  • Loading branch information
jukzi authored Sep 5, 2022
1 parent c7d94e3 commit f2f673b
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Matcher> fPositiveFileNameMatcher;
private final ThreadLocal<Matcher> fNegativeFileNameMatcher;

private boolean fVisitDerived;
private IWorkingSet[] fWorkingSets;
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f2f673b

Please sign in to comment.