Skip to content

Commit

Permalink
SONARJAVA-2140 LocksLockedCheck : report issue only on methodInvocati…
Browse files Browse the repository at this point in the history
…on trees
  • Loading branch information
benzonico authored and saberduck committed Feb 27, 2017
1 parent aebf5e8 commit e0474af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,12 @@ public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager co
}

private void reportIssue(JavaFileScannerContext.Location location) {
MethodInvocationTree syntaxNode = (MethodInvocationTree) location.syntaxNode;
String flowMsg = "Lock '" + SyntaxTreeNameFinder.getName(syntaxNode.methodSelect()) + "' is never unlocked.";
Tree tree = issueTree(syntaxNode);
reportIssue(tree, "Unlock this lock along all executions paths of this method.", FlowComputation.singleton(flowMsg, tree));
if (location.syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree syntaxNode = (MethodInvocationTree) location.syntaxNode;
String flowMsg = "Lock '" + SyntaxTreeNameFinder.getName(syntaxNode.methodSelect()) + "' is never unlocked.";
Tree tree = issueTree(syntaxNode);
reportIssue(tree, "Unlock this lock along all executions paths of this method.", FlowComputation.singleton(flowMsg, tree));
}
}

private static Tree issueTree(MethodInvocationTree syntaxNode) {
Expand Down
20 changes: 17 additions & 3 deletions java-frontend/src/test/files/se/LocksNotUnlockedCheck.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyClass {

Lock l1 = new ReentrantLock();
Expand Down Expand Up @@ -260,7 +259,7 @@ public void unlockingInTryCatchButMissed() {
}
}
}

private volatile ScheduledExecutorService executorService;
private final Runnable task = new Task();

Expand Down Expand Up @@ -289,7 +288,7 @@ public void unlockingInTryCatchButMissed() {
}
});
}

private final AbstractService delegate = new AbstractService() {
private volatile Future<?> runningTask;
private volatile ScheduledExecutorService executorService;
Expand Down Expand Up @@ -324,4 +323,19 @@ public void lock() {
}
}
}

public class SonarFail {
java.util.List<Lock> list;
Object test(Lock local) {
Lock l1 = list.get(0);
local.lock();
try {
if (l1 == local) {
return null;
}
} finally {
local.unlock();
}
}
}
}

0 comments on commit e0474af

Please sign in to comment.