Skip to content

Commit aa3f4e1

Browse files
committed
C#: Fix context-sensitive dispatch when using base qualifier
1 parent c2bb379 commit aa3f4e1

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ private module Internal {
270270
hasOverrider(t, c)
271271
}
272272

273+
/**
274+
* For `base` expressions, the extractor provides the type of the base
275+
* class instead of the derived class; this predicate provides the latter.
276+
*/
277+
private Type getBaseAdjustedType(BaseAccess base) {
278+
result = base.getEnclosingCallable().getDeclaringType()
279+
}
280+
273281
abstract private class DispatchOverridableCall extends DispatchCallImpl {
274282
pragma[noinline]
275283
OverridableCallable getAStaticTargetExt() {
@@ -360,7 +368,12 @@ private module Internal {
360368
private predicate contextArgHasType(DispatchCall ctx, Type t, boolean isExact) {
361369
exists(Expr arg, int i |
362370
this.relevantContext(ctx, i) and
363-
t = getAPossibleType(arg, isExact)
371+
(
372+
t = getBaseAdjustedType(arg) and isExact = false
373+
or
374+
not exists(getBaseAdjustedType(arg)) and
375+
t = getAPossibleType(arg, isExact)
376+
)
364377
|
365378
ctx.getArgument(i) = arg
366379
or
@@ -725,9 +738,7 @@ private module Internal {
725738

726739
Type getType(boolean isExact) {
727740
result = this.getType() and
728-
if
729-
this instanceof ObjectCreation or
730-
this instanceof BaseAccess
741+
if this instanceof ObjectCreation or this instanceof BaseAccess
731742
then isExact = true
732743
else isExact = false
733744
}

csharp/ql/test/library-tests/dataflow/call-sensitivity/CallSensitivityFlow.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ edges
6060
| CallSensitivityFlow.cs:187:17:187:30 | call to method CallMOut : Object | CallSensitivityFlow.cs:187:13:187:13 | access to local variable o : Object | provenance | |
6161
| CallSensitivityFlow.cs:205:40:205:40 | o : Object | CallSensitivityFlow.cs:208:18:208:18 | access to parameter o | provenance | |
6262
| CallSensitivityFlow.cs:239:35:239:35 | o : Object | CallSensitivityFlow.cs:241:17:241:17 | access to parameter o : Object | provenance | |
63-
| CallSensitivityFlow.cs:241:17:241:17 | access to parameter o : Object | CallSensitivityFlow.cs:244:35:244:35 | o : Object | provenance | |
64-
| CallSensitivityFlow.cs:244:35:244:35 | o : Object | CallSensitivityFlow.cs:246:14:246:14 | access to parameter o | provenance | |
63+
| CallSensitivityFlow.cs:241:17:241:17 | access to parameter o : Object | CallSensitivityFlow.cs:256:36:256:36 | o : Object | provenance | |
64+
| CallSensitivityFlow.cs:256:36:256:36 | o : Object | CallSensitivityFlow.cs:258:14:258:14 | access to parameter o | provenance | |
6565
| CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | CallSensitivityFlow.cs:239:35:239:35 | o : Object | provenance | |
6666
nodes
6767
| CallSensitivityFlow.cs:7:38:7:38 | o : Object | semmle.label | o : Object |
@@ -138,8 +138,8 @@ nodes
138138
| CallSensitivityFlow.cs:208:18:208:18 | access to parameter o | semmle.label | access to parameter o |
139139
| CallSensitivityFlow.cs:239:35:239:35 | o : Object | semmle.label | o : Object |
140140
| CallSensitivityFlow.cs:241:17:241:17 | access to parameter o : Object | semmle.label | access to parameter o : Object |
141-
| CallSensitivityFlow.cs:244:35:244:35 | o : Object | semmle.label | o : Object |
142-
| CallSensitivityFlow.cs:246:14:246:14 | access to parameter o | semmle.label | access to parameter o |
141+
| CallSensitivityFlow.cs:256:36:256:36 | o : Object | semmle.label | o : Object |
142+
| CallSensitivityFlow.cs:258:14:258:14 | access to parameter o | semmle.label | access to parameter o |
143143
| CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | semmle.label | object creation of type Object : Object |
144144
subpaths
145145
| CallSensitivityFlow.cs:85:26:85:37 | object creation of type Object : Object | CallSensitivityFlow.cs:7:38:7:38 | o : Object | CallSensitivityFlow.cs:11:20:11:20 | access to parameter o : Object | CallSensitivityFlow.cs:85:14:85:44 | call to method FlowThrough |
@@ -165,4 +165,4 @@ subpaths
165165
| CallSensitivityFlow.cs:172:37:172:48 | object creation of type Object : Object | CallSensitivityFlow.cs:172:37:172:48 | object creation of type Object : Object | CallSensitivityFlow.cs:188:14:188:14 | access to local variable o | $@ | CallSensitivityFlow.cs:188:14:188:14 | access to local variable o | access to local variable o |
166166
| CallSensitivityFlow.cs:182:21:182:32 | object creation of type Object : Object | CallSensitivityFlow.cs:182:21:182:32 | object creation of type Object : Object | CallSensitivityFlow.cs:208:18:208:18 | access to parameter o | $@ | CallSensitivityFlow.cs:208:18:208:18 | access to parameter o | access to parameter o |
167167
| CallSensitivityFlow.cs:185:21:185:32 | object creation of type Object : Object | CallSensitivityFlow.cs:185:21:185:32 | object creation of type Object : Object | CallSensitivityFlow.cs:164:14:164:14 | access to parameter o | $@ | CallSensitivityFlow.cs:164:14:164:14 | access to parameter o | access to parameter o |
168-
| CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | CallSensitivityFlow.cs:246:14:246:14 | access to parameter o | $@ | CallSensitivityFlow.cs:246:14:246:14 | access to parameter o | access to parameter o |
168+
| CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | CallSensitivityFlow.cs:263:17:263:28 | object creation of type Object : Object | CallSensitivityFlow.cs:258:14:258:14 | access to parameter o | $@ | CallSensitivityFlow.cs:258:14:258:14 | access to parameter o | access to parameter o |

0 commit comments

Comments
 (0)