Skip to content

Commit e4ee7c9

Browse files
committed
C#: Address review comments.
1 parent 5d63b6e commit e4ee7c9

File tree

8 files changed

+31
-21
lines changed

8 files changed

+31
-21
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/ObjectInitMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private ObjectInitMethod(Context cx, Type containingType)
1313
this.ContainingType = containingType;
1414
}
1515

16-
public static readonly string Name = "<object initializer>";
16+
private static readonly string Name = "<object initializer>";
1717

1818
public static ObjectInitMethod Create(Context cx, Type containingType)
1919
{

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ private module Initializers {
1414
* A non-static member with an initializer, for example a field `int Field = 0`.
1515
*/
1616
class InitializedInstanceMember extends Member {
17+
private AssignExpr ae;
18+
1719
InitializedInstanceMember() {
18-
exists(AssignExpr ae |
19-
not this.isStatic() and
20-
expr_parent_top_level(ae, _, this) and
21-
not ae = any(Callable c).getExpressionBody()
22-
)
20+
not this.isStatic() and
21+
expr_parent_top_level(ae, _, this) and
22+
not ae = any(Callable c).getExpressionBody()
2323
}
2424

2525
/** Gets the initializer expression. */
26-
AssignExpr getInitializer() { expr_parent_top_level(result, _, this) }
26+
AssignExpr getInitializer() { result = ae }
2727
}
2828

2929
/**
@@ -54,7 +54,7 @@ private module Initializers {
5454
}
5555

5656
/**
57-
* Gets the last member initializer expression for non-static constructor `c`
57+
* Gets the last member initializer expression for object initializer method `obinit`
5858
* in compilation `comp`.
5959
*/
6060
AssignExpr lastInitializer(ObjectInitMethod obinit, CompilationExt comp) {
@@ -224,6 +224,13 @@ predicate scopeLast(CfgScope scope, AstNode last, Completion c) {
224224
or
225225
last(callable.(Constructor).getInitializer(), last, c) and
226226
not callable.hasBody()
227+
or
228+
// This is only relevant in the context of compilation errors, since
229+
// normally the existence of an object initializer call implies the
230+
// existence of an initializer.
231+
last(callable.(Constructor).getObjectInitializerCall(), last, c) and
232+
not callable.(Constructor).hasInitializer() and
233+
not callable.hasBody()
227234
)
228235
or
229236
last(Initializers::lastInitializer(scope, _), last, c)
@@ -278,8 +285,15 @@ private class ConstructorTree extends ControlFlowTree instanceof Constructor {
278285
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
279286
exists(CompilationExt comp |
280287
last(this.getObjectInitializerCall(comp), pred, c) and
281-
c instanceof NormalCompletion and
288+
c instanceof NormalCompletion
289+
|
282290
first(this.getInitializer(comp), succ)
291+
or
292+
// This is only relevant in the context of compilation errors, since
293+
// normally the existence of an object initializer call implies the
294+
// existence of an initializer.
295+
not exists(this.getInitializer(comp)) and
296+
first(this.getBody(comp), succ)
283297
)
284298
}
285299
}

csharp/ql/test/library-tests/obinit/Flow.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edges
66
| 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 | |
77
| 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 | |
88
| 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 | |
9+
flow
10+
| obinit.cs:21:18:21:20 | access to field s |
911
nodes
1012
| obinit.cs:5:23:5:23 | [post] this access : A [field s] : String | semmle.label | [post] this access : A [field s] : String |
1113
| obinit.cs:5:27:5:34 | "source" : String | semmle.label | "source" : String |
@@ -16,5 +18,3 @@ nodes
1618
| 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 |
1719
| obinit.cs:21:18:21:20 | access to field s | semmle.label | access to field s |
1820
subpaths
19-
#select
20-
| obinit.cs:21:18:21:20 | access to field s |

csharp/ql/test/library-tests/obinit/Flow.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ module Flow = DataFlow::Global<FlowConfig>;
1717

1818
import Flow::PathGraph
1919

20-
from DataFlow::Node source, DataFlow::Node sink
21-
where Flow::flow(source, sink)
22-
select sink
20+
query predicate flow(DataFlow::Node sink) { Flow::flowTo(sink) }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Flow.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

csharp/ql/test/library-tests/obinit/obinit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ static void Sink(string s) { }
1818

1919
static void Foo() {
2020
A a = new A();
21-
Sink(a.s);
21+
Sink(a.s); // $ flow
2222

2323
A a2 = new A(0, 0);
24-
Sink(a2.s);
24+
Sink(a2.s); // $ MISSING: flow
2525

2626
B b = new B();
27-
Sink(b.s);
27+
Sink(b.s); // $ MISSING: flow
2828
}
2929
}
3030
}

csharp/ql/test/library-tests/standalone/errorrecovery/CONSISTENCY/CfgConsistency.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

csharp/ql/test/query-tests/standalone/Likely Bugs/IncomparableEquals/CONSISTENCY/CfgConsistency.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)