Skip to content

Commit

Permalink
C#: Remove false-positive reflection calls in dataflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Dec 11, 2024
1 parent 0f5786e commit fc70024
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ class NonDelegateDataFlowCall extends DataFlowCall, TNonDelegateCall {
Callable getATarget(boolean static) {
result = dc.getADynamicTarget().getUnboundDeclaration() and static = false
or
result = dc.getAStaticTarget().getUnboundDeclaration() and static = true
result = dc.getAStaticTarget().getUnboundDeclaration() and
static = true and
// In reflection calls, _all_ methods with matching names and arities are considered
// static targets, so we need to exclude them
not dc.isReflection()
}

override ControlFlow::Nodes::ElementNode getControlFlowNode() { result = cfn }
Expand Down
16 changes: 15 additions & 1 deletion csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@ class DispatchCall extends Internal::TDispatchCall {
}

/** Holds if this call uses reflection. */
predicate isReflection() { this instanceof Internal::TDispatchReflectionCall }
predicate isReflection() {
this instanceof Internal::TDispatchReflectionCall
or
this instanceof Internal::TDispatchDynamicElementAccess
or
this instanceof Internal::TDispatchDynamicMemberAccess
or
this instanceof Internal::TDispatchDynamicMethodCall
or
this instanceof Internal::TDispatchDynamicOperatorCall
or
this instanceof Internal::TDispatchDynamicEventAccess
or
this instanceof Internal::TDispatchDynamicObjectCreation
}
}

/** Internal implementation details. */
Expand Down

0 comments on commit fc70024

Please sign in to comment.