Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Remove false-positive reflection calls in dataflow #18269

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading