Skip to content

Commit 64f0dfb

Browse files
committed
Fix code review findings
1 parent 7d62e33 commit 64f0dfb

File tree

5 files changed

+31
-55
lines changed

5 files changed

+31
-55
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,8 @@ class DelegateLikeCallArgumentCallContext extends ArgumentCallContext {
8282

8383
/** An argument of a delegate call. */
8484
class DelegateCallArgumentCallContext extends DelegateLikeCallArgumentCallContext,
85-
TArgDelegateCallContext {
86-
DelegateCallArgumentCallContext() { this = TArgDelegateCallContext(dc, arg) }
87-
}
85+
TArgDelegateCallContext { }
8886

8987
/** An argument of a function pointer call. */
9088
class FunctionPointerCallArgumentCallContext extends DelegateLikeCallArgumentCallContext,
91-
TArgFunctionPointerCallContext {
92-
FunctionPointerCallArgumentCallContext() { this = TArgFunctionPointerCallContext(dc, arg) }
93-
}
89+
TArgFunctionPointerCallContext { }

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private module Cached {
101101
TNonDelegateCall(ControlFlow::Nodes::ElementNode cfn, DispatchCall dc) {
102102
cfn.getElement() = dc.getCall()
103103
} or
104-
TExplicitDelegateCall(ControlFlow::Nodes::ElementNode cfn, DelegateCall dc) {
104+
TExplicitDelegateLikeCall(ControlFlow::Nodes::ElementNode cfn, DelegateLikeCall dc) {
105105
cfn.getElement() = dc
106106
} or
107107
TTransitiveCapturedCall(ControlFlow::Nodes::ElementNode cfn, Callable target) {
@@ -308,12 +308,12 @@ abstract class DelegateDataFlowCall extends DataFlowCall {
308308
override DataFlowCallable getARuntimeTarget() { result = this.getARuntimeTarget(_) }
309309
}
310310

311-
/** An explicit delegate call relevant for data flow. */
312-
class ExplicitDelegateDataFlowCall extends DelegateDataFlowCall, TExplicitDelegateCall {
311+
/** An explicit delegate or function pointer call relevant for data flow. */
312+
class ExplicitDelegateLikeDataFlowCall extends DelegateDataFlowCall, TExplicitDelegateLikeCall {
313313
private ControlFlow::Nodes::ElementNode cfn;
314-
private DelegateCall dc;
314+
private DelegateLikeCall dc;
315315

316-
ExplicitDelegateDataFlowCall() { this = TExplicitDelegateCall(cfn, dc) }
316+
ExplicitDelegateLikeDataFlowCall() { this = TExplicitDelegateLikeCall(cfn, dc) }
317317

318318
override DataFlowCallable getARuntimeTarget(CallContext::CallContext cc) {
319319
result = getCallableForDataFlow(dc.getARuntimeTarget(cc))

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ private module OutNodes {
13941394

13951395
private DataFlowCall csharpCall(Expr e, ControlFlow::Node cfn) {
13961396
e = any(DispatchCall dc | result = TNonDelegateCall(cfn, dc)).getCall() or
1397-
result = TExplicitDelegateCall(cfn, e)
1397+
result = TExplicitDelegateLikeCall(cfn, e)
13981398
}
13991399

14001400
/** A valid return type for a method that uses `yield return`. */

csharp/ql/src/semmle/code/csharp/dataflow/internal/DelegateDataFlow.qll

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ private import semmle.code.csharp.dispatch.Dispatch
1515
private import semmle.code.csharp.frameworks.system.linq.Expressions
1616

1717
/** A source of flow for a delegate or function pointer expression. */
18-
private class DelegateLikeFlowSource extends DataFlow::ExprNode {
18+
abstract private class DelegateLikeFlowSource extends DataFlow::ExprNode {
1919
/** Gets the callable that is referenced in this delegate or function pointer flow source. */
20-
Callable getCallable() { none() }
20+
abstract Callable getCallable();
2121
}
2222

2323
/** A source of flow for a delegate expression. */
@@ -41,10 +41,13 @@ private class FunctionPointerFlowSource extends DelegateLikeFlowSource {
4141
Callable c;
4242

4343
FunctionPointerFlowSource() {
44-
this.getExpr() =
45-
any(Expr e |
46-
c = e.(AddressOfExpr).getOperand().(CallableAccess).getTarget().getUnboundDeclaration()
47-
)
44+
c =
45+
this.getExpr()
46+
.(AddressOfExpr)
47+
.getOperand()
48+
.(CallableAccess)
49+
.getTarget()
50+
.getUnboundDeclaration()
4851
}
4952

5053
/** Gets the callable that is referenced in this function pointer flow source. */
@@ -115,28 +118,12 @@ abstract private class DelegateLikeFlowSink extends DataFlow::Node {
115118

116119
/** A delegate or function pointer call expression. */
117120
class DelegateLikeCallExpr extends DelegateLikeFlowSink, DataFlow::ExprNode {
118-
/** Gets the delegate or function pointer call that this expression belongs to. */
119-
DelegateLikeCall getCall() { none() }
120-
}
121-
122-
/** A delegate call expression. */
123-
class DelegateCallExpr extends DelegateLikeCallExpr {
124-
DelegateCall dc;
121+
DelegateLikeCall dc;
125122

126-
DelegateCallExpr() { this.getExpr() = dc.getExpr() }
123+
DelegateLikeCallExpr() { this.getExpr() = dc.getExpr() }
127124

128-
/** Gets the delegate call that this expression belongs to. */
129-
override DelegateCall getCall() { result = dc }
130-
}
131-
132-
/** A function pointer call expression. */
133-
class FunctionPointerCallExpr extends DelegateLikeCallExpr {
134-
FunctionPointerCall fptrc;
135-
136-
FunctionPointerCallExpr() { this.getExpr() = fptrc.getExpr() }
137-
138-
/** Gets the function pointer call that this expression belongs to. */
139-
override FunctionPointerCall getCall() { result = fptrc }
125+
/** Gets the delegate or function pointer call that this expression belongs to. */
126+
DelegateLikeCall getCall() { result = dc }
140127
}
141128

142129
/** A parameter of delegate type belonging to a callable with a flow summary. */

csharp/ql/src/semmle/code/csharp/exprs/Call.qll

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,24 @@ class MutatorOperatorCall extends OperatorCall {
527527
predicate isPostfix() { mutator_invocation_mode(this, 2) }
528528
}
529529

530+
private class DelegateLikeCall_ = @delegate_invocation_expr or @function_pointer_invocation_expr;
531+
530532
/**
531533
* A function pointer or delegate call.
532534
*/
533-
abstract class DelegateLikeCall extends Call {
535+
class DelegateLikeCall extends Call, DelegateLikeCall_ {
534536
override Callable getTarget() { none() }
535537

536538
/**
537539
* Gets a potential run-time target of this delegate or function pointer call in the given
538540
* call context `cc`.
539541
*/
540-
Callable getARuntimeTarget(CallContext::CallContext cc) { none() }
542+
Callable getARuntimeTarget(CallContext::CallContext cc) {
543+
exists(DelegateLikeCallExpr call |
544+
this = call.getCall() and
545+
result = call.getARuntimeTarget(cc)
546+
)
547+
}
541548

542549
/**
543550
* Gets the delegate or function pointer expression of this call. For example, the
@@ -579,10 +586,7 @@ class DelegateCall extends DelegateLikeCall, @delegate_invocation_expr {
579586
* call context `cc`.
580587
*/
581588
override Callable getARuntimeTarget(CallContext::CallContext cc) {
582-
exists(DelegateCallExpr call |
583-
this = call.getCall() and
584-
result = call.getARuntimeTarget(cc)
585-
)
589+
result = DelegateLikeCall.super.getARuntimeTarget(cc)
586590
or
587591
exists(AddEventSource aes, CallContext::CallContext cc2 |
588592
aes = this.getAnAddEventSource(_) and
@@ -634,17 +638,6 @@ class DelegateCall extends DelegateLikeCall, @delegate_invocation_expr {
634638
* ```
635639
*/
636640
class FunctionPointerCall extends DelegateLikeCall, @function_pointer_invocation_expr {
637-
/**
638-
* Gets a potential run-time target of this function pointer call in the given
639-
* call context `cc`.
640-
*/
641-
override Callable getARuntimeTarget(CallContext::CallContext cc) {
642-
exists(FunctionPointerCallExpr call |
643-
this = call.getCall() and
644-
result = call.getARuntimeTarget(cc)
645-
)
646-
}
647-
648641
override string toString() { result = "function pointer call" }
649642

650643
override string getAPrimaryQlClass() { result = "FunctionPointerCall" }

0 commit comments

Comments
 (0)