Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 713c7a8

Browse files
committed
C#: Refactor TCallableFlowSourceDelegateArg
1 parent 18ff458 commit 713c7a8

1 file changed

Lines changed: 40 additions & 42 deletions

File tree

csharp/ql/src/semmle/code/csharp/dataflow/LibraryTypeDataFlow.qll

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ private module Cached {
6666
exists(LibraryTypeDataFlow ltdf, CallableFlowSourceDelegateArg csource, CallableFlowSink csink |
6767
ltdf.callableFlow(csource, csink, callable, preservesValue) and
6868
call.getTarget().getSourceDeclaration() = callable and
69-
csource.getCallable() = callable and
70-
csource.getArgumentIndex() = i and
69+
csource = getDelegateFlowSourceArg(callable, i) and
7170
sink = csink.getSink(call)
7271
)
7372
}
@@ -109,8 +108,7 @@ private module Cached {
109108
|
110109
ltdf.callableFlow(csource, csink, callable, preservesValue) and
111110
call.getTarget().getSourceDeclaration() = callable and
112-
csource.getCallable() = callable and
113-
csource.getArgumentIndex() = i and
111+
csource = getDelegateFlowSourceArg(callable, i) and
114112
csink.getCallable() = callable and
115113
csink.getDelegateIndex() = k and
116114
csink.getDelegateParameterIndex() = j
@@ -131,13 +129,7 @@ library class SourceDeclarationMethod extends SourceDeclarationCallable, Method
131129
private newtype TCallableFlowSource =
132130
TCallableFlowSourceQualifier() or
133131
TCallableFlowSourceArg(int i) { hasArgumentPosition(_, i) } or
134-
TCallableFlowSourceDelegateArg(SourceDeclarationCallable c, int i) {
135-
exists(DelegateType dt |
136-
dt = c.getParameter(i).getType().(SystemLinqExpressions::DelegateExtType).getDelegateType()
137-
|
138-
not dt.getReturnType() instanceof VoidType
139-
)
140-
}
132+
TCallableFlowSourceDelegateArg(int i) { hasDelegateArgumentPosition(_, i) }
141133

142134
private predicate hasArgumentPosition(SourceDeclarationCallable callable, int position) {
143135
exists(int arity |
@@ -154,6 +146,14 @@ private predicate hasArgumentPosition(SourceDeclarationCallable callable, int po
154146
)
155147
}
156148

149+
private predicate hasDelegateArgumentPosition(SourceDeclarationCallable c, int i) {
150+
exists(DelegateType dt |
151+
dt = c.getParameter(i).getType().(SystemLinqExpressions::DelegateExtType).getDelegateType()
152+
|
153+
not dt.getReturnType() instanceof VoidType
154+
)
155+
}
156+
157157
/** A flow source in a call to a library callable. */
158158
class CallableFlowSource extends TCallableFlowSource {
159159
/** Gets a textual representation of this flow source. */
@@ -188,17 +188,10 @@ CallableFlowSourceArg getFlowSourceArg(Callable callable, int i) {
188188

189189
/** A flow source in a call to a library callable: output from delegate argument. */
190190
class CallableFlowSourceDelegateArg extends CallableFlowSource, TCallableFlowSourceDelegateArg {
191-
override string toString() {
192-
exists(int i | this = TCallableFlowSourceDelegateArg(_, i) |
193-
result = "output from argument " + i.toString()
194-
)
195-
}
196-
197-
/** Gets the callable containing this flow source. */
198-
Callable getCallable() { this = TCallableFlowSourceDelegateArg(result, _) }
191+
override string toString() { result = "output from argument " + getArgumentIndex().toString() }
199192

200193
/** Gets the index of this delegate argument. */
201-
int getArgumentIndex() { this = TCallableFlowSourceDelegateArg(_, result) }
194+
int getArgumentIndex() { this = TCallableFlowSourceDelegateArg(result) }
202195

203196
override Expr getSource(Call c) { none() }
204197
}
@@ -258,6 +251,11 @@ CallableFlowSinkArg getFlowSinkArg(Callable callable, int i) {
258251
hasArgumentPosition(callable, i)
259252
}
260253

254+
CallableFlowSourceDelegateArg getDelegateFlowSourceArg(Callable callable, int i) {
255+
i = result.getArgumentIndex() and
256+
hasDelegateArgumentPosition(callable, i)
257+
}
258+
261259
/** The flow sink in a call to a library callable: parameter of a delegate argument. */
262260
class CallableFlowSinkDelegateArg extends CallableFlowSink, TCallableFlowSinkDelegateArg {
263261
override string toString() {
@@ -635,7 +633,7 @@ class SystemLazyFlow extends LibraryTypeDataFlow {
635633
exists(SystemFuncDelegateType t, int i | t.getNumberOfTypeParameters() = 1 |
636634
c.getDeclaringType() = this and
637635
c.getParameter(i).getType().getSourceDeclaration() = t and
638-
source = TCallableFlowSourceDelegateArg(c, i) and
636+
source = getDelegateFlowSourceArg(c, i) and
639637
sink = TCallableFlowSinkReturn()
640638
)
641639
}
@@ -697,7 +695,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
697695
source = TCallableFlowSourceArg(0) and
698696
sink = TCallableFlowSinkDelegateArg(m, 1, 1)
699697
or
700-
source = TCallableFlowSourceDelegateArg(m, 1) and
698+
source = TCallableFlowSourceDelegateArg(1) and
701699
sink = TCallableFlowSinkReturn()
702700
)
703701
or
@@ -709,7 +707,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
709707
source = TCallableFlowSourceArg(1) and
710708
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
711709
or
712-
source = TCallableFlowSourceDelegateArg(m, 2) and
710+
source = TCallableFlowSourceDelegateArg(2) and
713711
sink = TCallableFlowSinkReturn()
714712
)
715713
or
@@ -721,10 +719,10 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
721719
source = TCallableFlowSourceArg(1) and
722720
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
723721
or
724-
source = TCallableFlowSourceDelegateArg(m, 2) and
722+
source = TCallableFlowSourceDelegateArg(2) and
725723
sink = TCallableFlowSinkDelegateArg(m, 3, 0)
726724
or
727-
source = TCallableFlowSourceDelegateArg(m, 3) and
725+
source = TCallableFlowSourceDelegateArg(3) and
728726
sink = TCallableFlowSinkReturn()
729727
)
730728
)
@@ -846,12 +844,12 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
846844
sink = TCallableFlowSinkDelegateArg(m, 2, 1)
847845
or
848846
m.getParameter(2).getType().(ConstructedDelegateType).getNumberOfTypeArguments() = 3 and
849-
source = TCallableFlowSourceDelegateArg(m, 1) and
847+
source = getDelegateFlowSourceArg(m, 1) and
850848
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
851849
or
852850
not m.getParameter(2).getType().getSourceDeclaration() instanceof
853851
SystemCollectionsGenericIEqualityComparerTInterface and
854-
source = TCallableFlowSourceDelegateArg(m, 2) and
852+
source = getDelegateFlowSourceArg(m, 2) and
855853
sink = TCallableFlowSinkReturn()
856854
or
857855
m.getParameter(2).getType().getSourceDeclaration() instanceof
@@ -868,13 +866,13 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
868866
source = TCallableFlowSourceArg(0) and
869867
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
870868
or
871-
source = TCallableFlowSourceDelegateArg(m, 1) and
869+
source = getDelegateFlowSourceArg(m, 1) and
872870
sink = TCallableFlowSinkDelegateArg(m, 3, 0)
873871
or
874-
source = TCallableFlowSourceDelegateArg(m, 2) and
872+
source = getDelegateFlowSourceArg(m, 2) and
875873
sink = TCallableFlowSinkDelegateArg(m, 3, 1)
876874
or
877-
source = TCallableFlowSourceDelegateArg(m, 3) and
875+
source = getDelegateFlowSourceArg(m, 3) and
878876
sink = TCallableFlowSinkReturn()
879877
)
880878
)
@@ -895,7 +893,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
895893
source = TCallableFlowSourceArg(1) and
896894
sink = TCallableFlowSinkDelegateArg(m, 4, 1)
897895
or
898-
source = TCallableFlowSourceDelegateArg(m, 4) and
896+
source = TCallableFlowSourceDelegateArg(4) and
899897
sink = TCallableFlowSinkReturn()
900898
)
901899
)
@@ -982,7 +980,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
982980
source = TCallableFlowSourceArg(0) and
983981
sink = TCallableFlowSinkDelegateArg(m, 1, 0)
984982
or
985-
source = TCallableFlowSourceDelegateArg(m, 1) and
983+
source = TCallableFlowSourceDelegateArg(1) and
986984
sink = TCallableFlowSinkReturn()
987985
)
988986
)
@@ -997,10 +995,10 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
997995
source = TCallableFlowSourceArg(0) and
998996
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
999997
or
1000-
source = TCallableFlowSourceDelegateArg(m, 1) and
998+
source = TCallableFlowSourceDelegateArg(1) and
1001999
sink = TCallableFlowSinkDelegateArg(m, 2, 1)
10021000
or
1003-
source = TCallableFlowSourceDelegateArg(m, 2) and
1001+
source = TCallableFlowSourceDelegateArg(2) and
10041002
sink = TCallableFlowSinkReturn()
10051003
)
10061004
)
@@ -1064,7 +1062,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
10641062
source = TCallableFlowSourceArg(0) and
10651063
sink = TCallableFlowSinkDelegateArg(m, 2, 0)
10661064
or
1067-
source = TCallableFlowSourceDelegateArg(m, 2) and
1065+
source = getDelegateFlowSourceArg(m, 2) and
10681066
sink = TCallableFlowSinkReturn()
10691067
)
10701068
)
@@ -1103,7 +1101,7 @@ class IEnumerableFlow extends LibraryTypeDataFlow {
11031101
source = TCallableFlowSourceArg(1) and
11041102
sink = TCallableFlowSinkDelegateArg(m, 2, 1)
11051103
or
1106-
source = TCallableFlowSourceDelegateArg(m, 2) and
1104+
source = getDelegateFlowSourceArg(m, 2) and
11071105
sink = TCallableFlowSinkReturn()
11081106
)
11091107
)
@@ -1444,7 +1442,7 @@ class SystemThreadingTasksTaskFlow extends LibraryTypeDataFlow, SystemThreadingT
14441442
exists(ConstructedDelegateType func, int i |
14451443
m.getParameter(i).getType() = func and
14461444
func.getUnboundGeneric() instanceof SystemFuncDelegateType and
1447-
source = TCallableFlowSourceDelegateArg(m, i) and
1445+
source = getDelegateFlowSourceArg(m, i) and
14481446
sink = TCallableFlowSinkReturn()
14491447
)
14501448
)
@@ -1459,7 +1457,7 @@ class SystemThreadingTasksTaskFlow extends LibraryTypeDataFlow, SystemThreadingT
14591457
(
14601458
m.getReturnType() = any(SystemThreadingTasksTaskTClass c).getAConstructedGeneric() and
14611459
m.(UnboundGenericMethod).getNumberOfTypeParameters() = 1 and
1462-
source = TCallableFlowSourceDelegateArg(m, 0) and
1460+
source = TCallableFlowSourceDelegateArg(0) and
14631461
sink = TCallableFlowSinkReturn()
14641462
)
14651463
or
@@ -1502,7 +1500,7 @@ class SystemThreadingTasksTaskTFlow extends LibraryTypeDataFlow {
15021500
c.getDeclaringType() = this and
15031501
(
15041502
c.getParameter(0).getType() = any(SystemFuncDelegateType t).getAConstructedGeneric() and
1505-
source = TCallableFlowSourceDelegateArg(c, 0) and
1503+
source = TCallableFlowSourceDelegateArg(0) and
15061504
sink = TCallableFlowSinkReturn()
15071505
)
15081506
or
@@ -1549,7 +1547,7 @@ class SystemThreadingTasksTaskTFlow extends LibraryTypeDataFlow {
15491547
exists(ConstructedDelegateType func, int i |
15501548
m.getParameter(i).getType() = func and
15511549
func.getUnboundGeneric() instanceof SystemFuncDelegateType and
1552-
source = TCallableFlowSourceDelegateArg(m, i) and
1550+
source = getDelegateFlowSourceArg(m, i) and
15531551
sink = TCallableFlowSinkReturn()
15541552
)
15551553
)
@@ -1600,7 +1598,7 @@ class SystemThreadingTasksFactoryFlow extends LibraryTypeDataFlow {
16001598
exists(ConstructedDelegateType func, int i |
16011599
m.getParameter(i).getType() = func and
16021600
func.getUnboundGeneric() instanceof SystemFuncDelegateType and
1603-
source = TCallableFlowSourceDelegateArg(m, i) and
1601+
source = getDelegateFlowSourceArg(m, i) and
16041602
sink = TCallableFlowSinkReturn()
16051603
)
16061604
)
@@ -1624,7 +1622,7 @@ class SystemThreadingTasksFactoryFlow extends LibraryTypeDataFlow {
16241622
exists(ConstructedDelegateType func, int i |
16251623
m.getParameter(i).getType() = func and
16261624
func.getUnboundGeneric() instanceof SystemFuncDelegateType and
1627-
source = TCallableFlowSourceDelegateArg(m, i) and
1625+
source = getDelegateFlowSourceArg(m, i) and
16281626
sink = TCallableFlowSinkReturn()
16291627
)
16301628
)

0 commit comments

Comments
 (0)