Skip to content

Commit 7a99dfa

Browse files
committed
C#: Do flag missing Dispose calls on Task and Task<>.
1 parent f58c72e commit 7a99dfa

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

csharp/ql/src/API Abuse/NoDisposeCallOnLocalIDisposable.ql

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import csharp
1717
import Dispose
1818
import semmle.code.csharp.frameworks.System
19+
import semmle.code.csharp.frameworks.system.threading.Tasks
1920
import semmle.code.csharp.commons.Disposal
2021

2122
private class ReturnNode extends DataFlow::ExprNode {
@@ -24,15 +25,27 @@ private class ReturnNode extends DataFlow::ExprNode {
2425
}
2526
}
2627

28+
private class Task extends Type {
29+
Task() {
30+
this instanceof SystemThreadingTasksTaskClass or
31+
this instanceof SystemThreadingTasksTaskTClass
32+
}
33+
}
34+
2735
module DisposeCallOnLocalIDisposableConfig implements DataFlow::ConfigSig {
2836
predicate isSource(DataFlow::Node node) {
29-
node.asExpr() =
30-
any(LocalScopeDisposableCreation disposable |
31-
// Only care about library types - user types often have spurious IDisposable declarations
32-
disposable.getType().fromLibrary() and
33-
// WebControls are usually disposed automatically
34-
not disposable.getType() instanceof WebControl
35-
)
37+
exists(LocalScopeDisposableCreation disposable, Type t |
38+
node.asExpr() = disposable and
39+
t = disposable.getType()
40+
|
41+
// Only care about library types - user types often have spurious IDisposable declarations
42+
t.fromLibrary() and
43+
// WebControls are usually disposed automatically
44+
not t instanceof WebControl and
45+
// It is typically not nessesary to dispose tasks
46+
// https://devblogs.microsoft.com/pfxteam/do-i-need-to-dispose-of-tasks/
47+
not t instanceof Task
48+
)
3649
}
3750

3851
predicate isSink(DataFlow::Node node) {

0 commit comments

Comments
 (0)