Skip to content

Commit d055c85

Browse files
committed
C#: Exclude case from cs/dispose-not-called-on-throw where the disposable is disposed by a UsingStmt, even when explicitly disposed.
1 parent 7790ac4 commit d055c85

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

csharp/ql/src/API Abuse/DisposeNotCalledOnException.ql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ private class DisposeCall extends MethodCall {
5252
DisposeCall() { this.getTarget() instanceof DisposeMethod }
5353
}
5454

55+
private predicate localFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
56+
DataFlow::localFlowStep(nodeFrom, nodeTo) and
57+
not exists(AssignableDefinition def, UsingStmt uds |
58+
nodeTo.asExpr() = def.getAReachableRead() and
59+
def.getTargetAccess() = uds.getAVariableDeclExpr().getAccess()
60+
)
61+
}
62+
5563
private predicate reachesDisposeCall(DisposeCall disposeCall, DataFlow::Node node) {
56-
DataFlow::localFlowStep(node, DataFlow::exprNode(disposeCall.getQualifier()))
64+
localFlowStep(node, DataFlow::exprNode(disposeCall.getQualifier()))
5765
or
5866
exists(DataFlow::Node mid | reachesDisposeCall(disposeCall, mid) |
59-
DataFlow::localFlowStep(node, mid)
67+
localFlowStep(node, mid)
6068
)
6169
}
6270

csharp/ql/test/query-tests/API Abuse/DisposeNotCalledOnException/DisposeNotCalledOnException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public void Method()
6262
// GOOD: using declaration
6363
using SqlConnection c2 = new SqlConnection("");
6464
c2.Open();
65+
66+
// GOOD: Always disposed
67+
using SqlConnection c3 = new SqlConnection("");
68+
Throw2(c3);
69+
c3.Dispose();
70+
71+
// GOOD: Disposed automatically
72+
using (SqlConnection c4 = new SqlConnection(""))
73+
{
74+
Throw2(c4);
75+
c4.Dispose();
76+
}
6577
}
6678

6779
void Throw1(SqlConnection sc)

0 commit comments

Comments
 (0)