Skip to content

Commit 0a52420

Browse files
committed
C#: Add ContentDataFlow test
1 parent 2b2ac06 commit 0a52420

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
public class ContentFlow
4+
{
5+
public class A
6+
{
7+
public A FieldA;
8+
public B FieldB;
9+
}
10+
public class B
11+
{
12+
public A FieldA;
13+
public B FieldB;
14+
}
15+
16+
public void M(A a, B b)
17+
{
18+
var a1 = new A();
19+
Sink(a1.FieldA.FieldB);
20+
21+
a.FieldA.FieldB = new B();
22+
Sink(a);
23+
24+
var a2 = new A();
25+
b.FieldB.FieldA = a2.FieldB.FieldA;
26+
Sink(b);
27+
}
28+
29+
public static void Sink<T>(T t) { }
30+
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| ContentFlow.cs:18:18:18:24 | object creation of type A | field FieldB.field FieldA. | ContentFlow.cs:19:14:19:29 | access to field FieldB | | true |
2+
| ContentFlow.cs:21:27:21:33 | object creation of type B | | ContentFlow.cs:22:14:22:14 | access to parameter a | field FieldA.field FieldB. | true |
3+
| ContentFlow.cs:24:18:24:24 | object creation of type A | field FieldA.field FieldB. | ContentFlow.cs:26:14:26:14 | access to parameter b | field FieldB.field FieldA. | true |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import csharp
2+
import semmle.code.csharp.dataflow.internal.ContentDataFlow
3+
4+
class Conf extends ContentDataFlow::Configuration {
5+
Conf() { this = "ContentFlowConf" }
6+
7+
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof ObjectCreation }
8+
9+
override predicate isSink(DataFlow::Node sink) {
10+
exists(MethodCall mc |
11+
mc.getTarget().hasUndecoratedName("Sink") and
12+
mc.getAnArgument() = sink.asExpr()
13+
)
14+
}
15+
16+
override int accessPathLimit() { result = 2 }
17+
}
18+
19+
from
20+
Conf conf, ContentDataFlow::Node source, ContentDataFlow::AccessPath sourceAp,
21+
ContentDataFlow::Node sink, ContentDataFlow::AccessPath sinkAp, boolean preservesValue
22+
where conf.hasFlow(source, sourceAp, sink, sinkAp, preservesValue)
23+
select source, sourceAp, sink, sinkAp, preservesValue

0 commit comments

Comments
 (0)