Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Leak Checker: Add a check to avoid mapping multiple keys to a ternary expression for tempVarToTree #5137

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ protected ResourceLeakAnalysis createFlowAnalysis() {
* @param tree the tree of the expression the tempvar represents
*/
/* package-private */ void addTempVar(LocalVariableNode tmpVar, Tree tree) {
tempVarToTree.put(tmpVar, tree);
if (!tempVarToTree.containsValue(tree)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned about this change. If addTempVar() is being called, the caller has already created a new tmpVar for the tree. It seems problematic if tmpVar gets used elsewhere but doesn't get added to the tempVarToTree mapping. What do you think of this concern?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes any problem because each node has to have a unique tmpVar which is defined in MustCallAnnotatedTypeFactory. But the problem here is that in ResourceLeakTransfer in visitTernaryExpression we don't check if there exist a tmpVar for the node like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, assuming the tests pass, I am ok with this change for now. I'll finish reviewing once CI passes (I think there is a formatting error)

tempVarToTree.put(tmpVar, tree);
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions checker/tests/resourceleak/HDFSReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Handler extends Thread {
boolean running;

static class Call {
boolean isResponseDeferred() {
return true;
}
}

@Override
public void run() {
while (running) {
Call call = null;
try {
if (running) {
continue;
}
} catch (Exception e) {
} finally {
String s = call.isResponseDeferred() ? ", deferred" : "";
}
}
}
}