Skip to content

Commit

Permalink
Fix UnsupportedOperationException in AbstractRegexCheckTrackingMatche…
Browse files Browse the repository at this point in the history
…rs.onMethodInvocationFound (#3331)
  • Loading branch information
alban-auzeill authored Dec 7, 2020
1 parent 805686d commit 5b61d27
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public void test() {
f(compile("(?=a)ab").matcher("abc").find());
f(compile("(?=abc)ab").matcher("abc").matches()); // Noncompliant
f(compile("(?=abc)ab").matcher("abc").find());

// string regex and pattern regex share the same string literal
String pattern_str = "(?=a)b"; // Noncompliant
Pattern pattern = Pattern.compile(pattern_str);
f("".matches(pattern_str));
f(pattern.matcher("").matches());
}

abstract void f(Pattern pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private Optional<RegexParseResult> getRegexOperand(MethodInvocationTree mit) {
@Override
public void checkRegex(RegexParseResult regexForLiterals, ExpressionTree methodInvocationOrAnnotation) {
if (methodInvocationOrAnnotation.is(Tree.Kind.ANNOTATION)) {
methodsCalledOnRegex.put(regexForLiterals, Collections.emptyList());
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
return;
}
MethodInvocationTree mit = (MethodInvocationTree) methodInvocationOrAnnotation;
Expand All @@ -215,9 +215,9 @@ public void checkRegex(RegexParseResult regexForLiterals, ExpressionTree methodI
methodInvocationToRegex.put(mit, regexForLiterals);
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
} else if (trackedMethodMatchers().matches(mit)) {
methodsCalledOnRegex.put(regexForLiterals, Collections.singletonList(mit));
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>(Collections.singletonList(mit)));
} else {
methodsCalledOnRegex.put(regexForLiterals, Collections.emptyList());
methodsCalledOnRegex.put(regexForLiterals, new ArrayList<>());
}
}

Expand Down

0 comments on commit 5b61d27

Please sign in to comment.