C#: Add object initializer test.

This commit is contained in:
Anders Schack-Mulligen
2025-11-25 15:51:27 +01:00
parent 848677e580
commit a7066ec758
3 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
edges
| obinit.cs:5:23:5:23 | [post] this access : A [field s] : String | obinit.cs:7:16:7:16 | this [Return] : A [field s] : String | provenance | |
| obinit.cs:5:27:5:34 | "source" : String | obinit.cs:5:23:5:23 | [post] this access : A [field s] : String | provenance | |
| obinit.cs:7:16:7:16 | this [Return] : A [field s] : String | obinit.cs:20:19:20:25 | object creation of type A : A [field s] : String | provenance | |
| obinit.cs:20:15:20:15 | access to local variable a : A [field s] : String | obinit.cs:21:18:21:18 | access to local variable a : A [field s] : String | provenance | |
| obinit.cs:20:19:20:25 | object creation of type A : A [field s] : String | obinit.cs:20:15:20:15 | access to local variable a : A [field s] : String | provenance | |
| obinit.cs:21:18:21:18 | access to local variable a : A [field s] : String | obinit.cs:21:18:21:20 | access to field s | provenance | |
nodes
| obinit.cs:5:23:5:23 | [post] this access : A [field s] : String | semmle.label | [post] this access : A [field s] : String |
| obinit.cs:5:27:5:34 | "source" : String | semmle.label | "source" : String |
| obinit.cs:7:16:7:16 | this [Return] : A [field s] : String | semmle.label | this [Return] : A [field s] : String |
| obinit.cs:20:15:20:15 | access to local variable a : A [field s] : String | semmle.label | access to local variable a : A [field s] : String |
| obinit.cs:20:19:20:25 | object creation of type A : A [field s] : String | semmle.label | object creation of type A : A [field s] : String |
| obinit.cs:21:18:21:18 | access to local variable a : A [field s] : String | semmle.label | access to local variable a : A [field s] : String |
| obinit.cs:21:18:21:20 | access to field s | semmle.label | access to field s |
subpaths
#select
| obinit.cs:21:18:21:20 | access to field s |

View File

@@ -0,0 +1,22 @@
import csharp
module FlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source.asExpr().(StringLiteral).getValue() = "source"
}
predicate isSink(DataFlow::Node sink) {
exists(MethodCall mc |
mc.getTarget().getUndecoratedName() = "Sink" and
mc.getAnArgument() = sink.asExpr()
)
}
}
module Flow = DataFlow::Global<FlowConfig>;
import Flow::PathGraph
from DataFlow::Node source, DataFlow::Node sink
where Flow::flow(source, sink)
select sink

View File

@@ -0,0 +1,30 @@
namespace ObInit {
public class A {
int x = 1;
public string s = "source";
public A() { }
public A(int y) { }
public A(int y, int z) : this(y) { }
}
public class B : A {
public B() : base(10) { }
static void Sink(string s) { }
static void Foo() {
A a = new A();
Sink(a.s);
A a2 = new A(0, 0);
Sink(a2.s);
B b = new B();
Sink(b.s);
}
}
}