Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c31f0e9

Browse files
committed
C#: Add more flow-through data-flow tests
1 parent d0ac846 commit c31f0e9

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

csharp/ql/test/library-tests/dataflow/fields/FieldFlow.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ edges
174174
| G.cs:52:14:52:21 | access to field boxfield [Box1, Elem] | G.cs:52:14:52:26 | access to field Box1 [Elem] : Elem |
175175
| G.cs:52:14:52:21 | this access [boxfield, Box1, ... (3)] | G.cs:52:14:52:21 | access to field boxfield [Box1, Elem] |
176176
| G.cs:52:14:52:26 | access to field Box1 [Elem] : Elem | G.cs:52:14:52:31 | access to field Elem |
177+
| H.cs:88:17:88:17 | [post] access to local variable a [FieldA] : Object | H.cs:89:14:89:14 | access to local variable a [FieldA] : Object |
178+
| H.cs:88:20:88:31 | object creation of type Object : Object | H.cs:88:17:88:17 | [post] access to local variable a [FieldA] : Object |
179+
| H.cs:89:14:89:14 | access to local variable a [FieldA] : Object | H.cs:89:14:89:21 | access to field FieldA |
177180
nodes
178181
| A.cs:5:17:5:23 | object creation of type C : C | semmle.label | object creation of type C : C |
179182
| A.cs:6:17:6:25 | call to method Make [c] : C | semmle.label | call to method Make [c] : C |
@@ -371,6 +374,10 @@ nodes
371374
| G.cs:52:14:52:21 | this access [boxfield, Box1, ... (3)] | semmle.label | this access [boxfield, Box1, ... (3)] |
372375
| G.cs:52:14:52:26 | access to field Box1 [Elem] : Elem | semmle.label | access to field Box1 [Elem] : Elem |
373376
| G.cs:52:14:52:31 | access to field Elem | semmle.label | access to field Elem |
377+
| H.cs:88:17:88:17 | [post] access to local variable a [FieldA] : Object | semmle.label | [post] access to local variable a [FieldA] : Object |
378+
| H.cs:88:20:88:31 | object creation of type Object : Object | semmle.label | object creation of type Object : Object |
379+
| H.cs:89:14:89:14 | access to local variable a [FieldA] : Object | semmle.label | access to local variable a [FieldA] : Object |
380+
| H.cs:89:14:89:21 | access to field FieldA | semmle.label | access to field FieldA |
374381
#select
375382
| A.cs:7:14:7:16 | access to field c | A.cs:5:17:5:23 | object creation of type C : C | A.cs:7:14:7:16 | access to field c | $@ | A.cs:5:17:5:23 | object creation of type C : C | object creation of type C : C |
376383
| A.cs:14:14:14:20 | call to method Get | A.cs:13:15:13:22 | object creation of type C1 : C1 | A.cs:14:14:14:20 | call to method Get | $@ | A.cs:13:15:13:22 | object creation of type C1 : C1 | object creation of type C1 : C1 |
@@ -410,3 +417,4 @@ nodes
410417
| G.cs:39:14:39:35 | call to method GetElem | G.cs:23:18:23:27 | object creation of type Elem : Elem | G.cs:39:14:39:35 | call to method GetElem | $@ | G.cs:23:18:23:27 | object creation of type Elem : Elem | object creation of type Elem : Elem |
411418
| G.cs:39:14:39:35 | call to method GetElem | G.cs:31:18:31:27 | object creation of type Elem : Elem | G.cs:39:14:39:35 | call to method GetElem | $@ | G.cs:31:18:31:27 | object creation of type Elem : Elem | object creation of type Elem : Elem |
412419
| G.cs:52:14:52:31 | access to field Elem | G.cs:44:18:44:27 | object creation of type Elem : Elem | G.cs:52:14:52:31 | access to field Elem | $@ | G.cs:44:18:44:27 | object creation of type Elem : Elem | object creation of type Elem : Elem |
420+
| H.cs:89:14:89:21 | access to field FieldA | H.cs:88:20:88:31 | object creation of type Object : Object | H.cs:89:14:89:21 | access to field FieldA | $@ | H.cs:88:20:88:31 | object creation of type Object : Object | object creation of type Object : Object |
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
public class H
2+
{
3+
class A
4+
{
5+
public object FieldA;
6+
}
7+
8+
class B
9+
{
10+
public object FieldB;
11+
}
12+
13+
A Clone(A a)
14+
{
15+
var ret = new A();
16+
ret.FieldA = a.FieldA;
17+
return ret;
18+
}
19+
20+
void M1(object o)
21+
{
22+
var a = new A();
23+
a.FieldA = new object();
24+
var clone = Clone(a);
25+
Sink(clone.FieldA); // flow [MISSING]
26+
27+
a = new A();
28+
a.FieldA = o;
29+
clone = Clone(a);
30+
Sink(clone.FieldA); // no flow
31+
}
32+
33+
B Transform(A a)
34+
{
35+
var b = new B();
36+
b.FieldB = a.FieldA;
37+
return b;
38+
}
39+
40+
void M2(object o)
41+
{
42+
var a = new A();
43+
a.FieldA = new object();
44+
var b = Transform(a);
45+
Sink(b.FieldB); // flow [MISSING]
46+
47+
a = new A();
48+
a.FieldA = o;
49+
b = Transform(a);
50+
Sink(b.FieldB); // no flow
51+
}
52+
53+
void TransformArg(A a, B b1, B b2)
54+
{
55+
b1.FieldB = a.FieldA;
56+
}
57+
58+
void M3(object o)
59+
{
60+
var a = new A();
61+
var b1 = new B();
62+
var b2 = new B();
63+
a.FieldA = new object();
64+
TransformArg(a, b1, b2);
65+
Sink(b1.FieldB); // flow [MISSING]
66+
Sink(b2.FieldB); // no flow
67+
68+
a = new A();
69+
b1 = new B();
70+
b2 = new B();
71+
a.FieldA = o;
72+
TransformArg(a, b1, b2);
73+
Sink(b1.FieldB); // no flow
74+
Sink(b2.FieldB); // no flow
75+
}
76+
77+
void SetArgs(A a, object o, B b1, B b2)
78+
{
79+
a.FieldA = o;
80+
TransformArg(a, b1, b2);
81+
}
82+
83+
void M4(object o)
84+
{
85+
var a = new A();
86+
var b1 = new B();
87+
var b2 = new B();
88+
SetArgs(a, new object(), b1, b2);
89+
Sink(a.FieldA); // flow
90+
Sink(b1.FieldB); // flow [MISSING]
91+
Sink(b2.FieldB); // no flow
92+
93+
a = new A();
94+
b1 = new B();
95+
b2 = new B();
96+
SetArgs(a, o, b1, b2);
97+
Sink(a.FieldA); // no flow
98+
Sink(b1.FieldB); // no flow
99+
Sink(b2.FieldB); // no flow
100+
}
101+
102+
B TransformWrap(A a)
103+
{
104+
var temp = new B();
105+
temp.FieldB = a;
106+
return Transform((A)temp.FieldB);
107+
}
108+
109+
void M5(object o)
110+
{
111+
var a = new A();
112+
a.FieldA = new object();
113+
var b = TransformWrap(a);
114+
Sink(b.FieldB); // flow [MISSING]
115+
116+
a = new A();
117+
a.FieldA = o;
118+
b = TransformWrap(a);
119+
Sink(b.FieldB); // no flow
120+
}
121+
122+
object Get(A a)
123+
{
124+
return Transform(a).FieldB;
125+
}
126+
127+
void M6(object o)
128+
{
129+
var a = new A();
130+
a.FieldA = new object();
131+
Sink(Get(a)); // flow [MISSING]
132+
133+
a = new A();
134+
a.FieldA = o;
135+
Sink(Get(a)); // no flow
136+
}
137+
138+
object Through(object o)
139+
{
140+
var a = new A();
141+
a.FieldA = o;
142+
return Transform(a).FieldB;
143+
}
144+
145+
void M7()
146+
{
147+
var o = Through(new object());
148+
Sink(o); // flow [MISSING]
149+
}
150+
151+
public static void Sink(object o) { }
152+
}

0 commit comments

Comments
 (0)