Skip to content

Commit 220526f

Browse files
michaelnebelhvitved
authored andcommitted
C#: Fix issues with summarized callables parameter types and other casting issues.
1 parent 2c414b2 commit 220526f

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ private import semmle.code.csharp.dispatch.RuntimeCallable
1212
private import semmle.code.csharp.frameworks.system.Collections
1313
private import semmle.code.csharp.frameworks.system.collections.Generic
1414

15-
private predicate summarizedCallable(DataFlowCallable c) {
16-
exists(c.asSummarizedCallable())
17-
or
18-
FlowSummaryImpl::Private::summaryReturnNode(_, TJumpReturnKind(c, _))
19-
or
20-
c.asCallable() = interpretElement(_, _, _, _, _, _)
21-
}
22-
2315
/**
2416
* Gets a source declaration of callable `c` that has a body or has
2517
* a flow summary.
@@ -29,9 +21,6 @@ private predicate summarizedCallable(DataFlowCallable c) {
2921
*/
3022
DotNet::Callable getCallableForDataFlow(DotNet::Callable c) {
3123
exists(DotNet::Callable unboundDecl | unboundDecl = c.getUnboundDeclaration() |
32-
summarizedCallable(TDotNetCallable(unboundDecl)) and
33-
result = unboundDecl
34-
or
3524
result.hasBody() and
3625
if unboundDecl.getFile().fromSource()
3726
then
@@ -329,9 +318,7 @@ class NonDelegateDataFlowCall extends DataFlowCall, TNonDelegateCall {
329318
override DataFlowCallable getARuntimeTarget() {
330319
result.asCallable() = getCallableForDataFlow(dc.getADynamicTarget())
331320
or
332-
result.asCallable() = dc.getAStaticTarget().getUnboundDeclaration() and
333-
summarizedCallable(result) and
334-
not result.asCallable() instanceof RuntimeCallable
321+
result.asSummarizedCallable() = dc.getAStaticTarget().getUnboundDeclaration()
335322
}
336323

337324
override ControlFlow::Nodes::ElementNode getControlFlowNode() { result = cfn }

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private import semmle.code.csharp.frameworks.system.Collections
1919
private import semmle.code.csharp.frameworks.system.threading.Tasks
2020

2121
/** Gets the callable in which this node occurs. */
22-
DataFlowCallable nodeGetEnclosingCallable(Node n) { result.asCallable() = n.getEnclosingCallable() }
22+
DataFlowCallable nodeGetEnclosingCallable(NodeImpl n) { result = n.getEnclosingCallableImpl() }
2323

2424
/** Holds if `p` is a `ParameterNode` of `c` with position `pos`. */
2525
predicate isParameterNode(ParameterNodeImpl p, DataFlowCallable c, ParameterPosition pos) {
@@ -980,7 +980,13 @@ private module ParameterNodes {
980980

981981
override DataFlowCallable getEnclosingCallableImpl() { result = sc }
982982

983-
override Type getTypeImpl() { result instanceof ObjectType }
983+
override Type getTypeImpl() {
984+
exists(int i |
985+
pos_.getPosition() = i and result = sc.asSummarizedCallable().getParameter(i).getType()
986+
)
987+
or
988+
pos_.isThisParameter() and result = sc.asSummarizedCallable().getDeclaringType()
989+
}
984990

985991
override ControlFlow::Node getControlFlowNodeImpl() { none() }
986992

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,29 @@ DataFlowType getContentType(Content c) {
3838
)
3939
}
4040

41-
private DataFlowType getReturnTypeBase(DataFlowCallable c, ReturnKind rk) {
41+
private DataFlowType getReturnTypeBase(DotNet::Callable c, ReturnKind rk) {
4242
exists(Type t | result = Gvn::getGlobalValueNumber(t) |
4343
rk instanceof NormalReturnKind and
4444
(
45-
t = c.asCallable().(Constructor).getDeclaringType()
45+
t = c.(Constructor).getDeclaringType()
4646
or
47-
not c.asCallable() instanceof Constructor and
48-
t = c.asCallable().getReturnType()
47+
not c instanceof Constructor and
48+
t = c.getReturnType()
4949
)
5050
or
51-
t = c.asCallable().getParameter(rk.(OutRefReturnKind).getPosition()).getType()
51+
t = c.getParameter(rk.(OutRefReturnKind).getPosition()).getType()
5252
)
5353
}
5454

5555
/** Gets the return type of kind `rk` for callable `c`. */
5656
bindingset[c]
5757
DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) {
58-
result = getReturnTypeBase(c, rk)
58+
result = getReturnTypeBase(c.asSummarizedCallable(), rk)
5959
or
6060
rk =
61-
any(JumpReturnKind jrk | result = getReturnTypeBase(jrk.getTarget(), jrk.getTargetReturnKind()))
61+
any(JumpReturnKind jrk |
62+
result = getReturnTypeBase(jrk.getTarget().asCallable(), jrk.getTargetReturnKind())
63+
)
6264
}
6365

6466
/**

0 commit comments

Comments
 (0)