Skip to content

Commit 1bcac50

Browse files
committed
C#: Add support for flow through side-effects on static fields
1 parent 29c204b commit 1bcac50

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: majorAnalysis
3+
---
4+
* Added support for data flow through side-effects on static fields. For example, when a static field containing an array is updated.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,9 +2095,11 @@ predicate jumpStep(Node pred, Node succ) {
20952095
f.getAnAssignedValue() = pred.asExpr() and
20962096
succ = TFlowInsensitiveFieldNode(f)
20972097
or
2098-
exists(FieldOrPropertyRead fr |
2098+
exists(FieldOrPropertyRead fr | f.getAnAccess() = fr |
2099+
fr = pred.(PostUpdateNode).getPreUpdateNode().asExpr() and
2100+
succ = TFlowInsensitiveFieldNode(f)
2101+
or
20992102
pred = TFlowInsensitiveFieldNode(f) and
2100-
f.getAnAccess() = fr and
21012103
fr = succ.asExpr() and
21022104
fr.hasNonlocalValue()
21032105
)

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,16 @@ edges
11521152
| J.cs:125:14:125:14 | access to local variable a : Int32[] [element] : Int32 | J.cs:125:14:125:17 | access to array element : Int32 | provenance | |
11531153
| J.cs:125:14:125:17 | access to array element : Int32 | J.cs:125:14:125:17 | (...) ... | provenance | |
11541154
| J.cs:125:14:125:17 | access to array element : Int32 | J.cs:125:14:125:17 | (...) ... | provenance | |
1155+
| K.cs:7:13:7:13 | access to local variable o : String | K.cs:8:22:8:22 | access to local variable o : String | provenance | |
1156+
| K.cs:7:13:7:13 | access to local variable o : String | K.cs:8:22:8:22 | access to local variable o : String | provenance | |
1157+
| K.cs:7:17:7:33 | call to method Source<String> : String | K.cs:7:13:7:13 | access to local variable o : String | provenance | |
1158+
| K.cs:7:17:7:33 | call to method Source<String> : String | K.cs:7:13:7:13 | access to local variable o : String | provenance | |
1159+
| K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | provenance | |
1160+
| K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | provenance | |
1161+
| K.cs:8:22:8:22 | access to local variable o : String | K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | provenance | |
1162+
| K.cs:8:22:8:22 | access to local variable o : String | K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | provenance | |
1163+
| K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | K.cs:13:14:13:23 | access to array element | provenance | |
1164+
| K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | K.cs:13:14:13:23 | access to array element | provenance | |
11551165
nodes
11561166
| A.cs:5:13:5:13 | access to local variable c : C | semmle.label | access to local variable c : C |
11571167
| A.cs:5:13:5:13 | access to local variable c : C | semmle.label | access to local variable c : C |
@@ -2393,6 +2403,18 @@ nodes
23932403
| J.cs:125:14:125:17 | (...) ... | semmle.label | (...) ... |
23942404
| J.cs:125:14:125:17 | access to array element : Int32 | semmle.label | access to array element : Int32 |
23952405
| J.cs:125:14:125:17 | access to array element : Int32 | semmle.label | access to array element : Int32 |
2406+
| K.cs:7:13:7:13 | access to local variable o : String | semmle.label | access to local variable o : String |
2407+
| K.cs:7:13:7:13 | access to local variable o : String | semmle.label | access to local variable o : String |
2408+
| K.cs:7:17:7:33 | call to method Source<String> : String | semmle.label | call to method Source<String> : String |
2409+
| K.cs:7:17:7:33 | call to method Source<String> : String | semmle.label | call to method Source<String> : String |
2410+
| K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | semmle.label | [post] access to field Strings : String[] [element] : String |
2411+
| K.cs:8:9:8:15 | [post] access to field Strings : String[] [element] : String | semmle.label | [post] access to field Strings : String[] [element] : String |
2412+
| K.cs:8:22:8:22 | access to local variable o : String | semmle.label | access to local variable o : String |
2413+
| K.cs:8:22:8:22 | access to local variable o : String | semmle.label | access to local variable o : String |
2414+
| K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | semmle.label | access to field Strings : String[] [element] : String |
2415+
| K.cs:13:14:13:20 | access to field Strings : String[] [element] : String | semmle.label | access to field Strings : String[] [element] : String |
2416+
| K.cs:13:14:13:23 | access to array element | semmle.label | access to array element |
2417+
| K.cs:13:14:13:23 | access to array element | semmle.label | access to array element |
23962418
subpaths
23972419
| A.cs:6:24:6:24 | access to local variable c : C | A.cs:147:32:147:32 | c : C | A.cs:149:20:149:27 | object creation of type B : B [field c] : C | A.cs:6:17:6:25 | call to method Make : B [field c] : C |
23982420
| A.cs:6:24:6:24 | access to local variable c : C | A.cs:147:32:147:32 | c : C | A.cs:149:20:149:27 | object creation of type B : B [field c] : C | A.cs:6:17:6:25 | call to method Make : B [field c] : C |
@@ -2647,3 +2669,5 @@ subpaths
26472669
| J.cs:107:14:107:17 | access to property Y | J.cs:105:32:105:48 | call to method Source<Object> : Object | J.cs:107:14:107:17 | access to property Y | $@ | J.cs:105:32:105:48 | call to method Source<Object> : Object | call to method Source<Object> : Object |
26482670
| J.cs:125:14:125:17 | (...) ... | J.cs:119:20:119:34 | call to method Source<Int32> : Int32 | J.cs:125:14:125:17 | (...) ... | $@ | J.cs:119:20:119:34 | call to method Source<Int32> : Int32 | call to method Source<Int32> : Int32 |
26492671
| J.cs:125:14:125:17 | (...) ... | J.cs:119:20:119:34 | call to method Source<Int32> : Int32 | J.cs:125:14:125:17 | (...) ... | $@ | J.cs:119:20:119:34 | call to method Source<Int32> : Int32 | call to method Source<Int32> : Int32 |
2672+
| K.cs:13:14:13:23 | access to array element | K.cs:7:17:7:33 | call to method Source<String> : String | K.cs:13:14:13:23 | access to array element | $@ | K.cs:7:17:7:33 | call to method Source<String> : String | call to method Source<String> : String |
2673+
| K.cs:13:14:13:23 | access to array element | K.cs:7:17:7:33 | call to method Source<String> : String | K.cs:13:14:13:23 | access to array element | $@ | K.cs:7:17:7:33 | call to method Source<String> : String | call to method Source<String> : String |

csharp/ql/test/library-tests/dataflow/fields/K.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private void M1()
1010

1111
private void M2()
1212
{
13-
Sink(Strings[0]); // $ MISSING: hasValueFlow=1
13+
Sink(Strings[0]); // $ hasValueFlow=1
1414
}
1515

1616
public static void Sink(object o) { }

0 commit comments

Comments
 (0)