Files
codeql/csharp/ql/test/library-tests/dataflow/fields/E.cs
Tom Hvitved d1755500e4 C#: Data flow through fields
Initial implementation of data flow through fields, using the algorithm of the
shared data flow implementation. Fields (and field-like properties) are covered,
and stores can be either
 - ordinary assignments, `Foo = x`,
 - object initializers, `new C() { Foo = x }`, or
 - field initializers, `int Foo = x`.

For field initializers, we need to synthesize calls (`SynthesizedCall`),
callables (`SynthesizedCallable`), parameters (`InstanceParameterNode`), and
arguments (`SynthesizedThisArgumentNode`), as the C# extractor does not (yet)
extract such entities. For example, in

```
class C
{
    int Field1 = 1;
    int Field2 = 2;

    C() { }
}
```

there is a synthesized call from the constructor `C`, with a synthesized `this`
argument, and the targets of that call are two synthesized callables with bodies
`this.Field1 = 1` and `this.Field2 = 2`, respectively.

A consequence of this is that `DataFlowCallable` is no longer an alias for
`DotNet::Callable`, but instead an IPA type.
2019-08-16 15:49:37 +02:00

33 lines
511 B
C#

public class E
{
struct S
{
public object Field;
}
static S CreateS(object o)
{
var ret = new S();
ret.Field = o;
return ret;
}
static void NotASetter(S s, object o)
{
s.Field = o;
}
private void M()
{
var o = new object();
var s = CreateS(o);
Sink(s.Field); // flow
s = new S();
NotASetter(s, o);
Sink(s.Field); // no flow
}
public static void Sink(object o) { }
}