Skip to content

Commit 7c473c3

Browse files
authored
Merge pull request github#17585 from hvitved/shared/cfg-scope-no-first-consistency
Shared: Add CFG consistency check for scopes with missing entry points
2 parents 7c32efc + f389a88 commit 7c473c3

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ class CfgScope extends Element, @top_level_exprorstmt_parent {
1515
CfgScope() {
1616
this.getFile().fromSource() and
1717
(
18-
this instanceof Callable
18+
this =
19+
any(Callable c |
20+
c.(Constructor).hasInitializer()
21+
or
22+
InitializerSplitting::constructorInitializes(c, _)
23+
or
24+
c.hasBody()
25+
)
1926
or
2027
// For now, static initializer values have their own scope. Eventually, they
2128
// should be treated like instance initializers.
22-
this.(Assignable).(Modifiable).isStatic()
29+
this.(Assignable).(Modifiable).isStatic() and
30+
expr_parent_top_level_adjusted2(_, _, this)
2331
)
2432
}
2533
}

ql/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ target
33
.cache
44
ql/test/**/*.testproj
55
ql/test/**/*.actual
6-
ql/test/**/CONSISTENCY
76
work

ruby/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ extractor/target
33
.cache
44
ql/test/**/*.testproj
55
ql/test/**/*.actual
6-
ql/test/**/CONSISTENCY
76
.codeql

ruby/ql/consistency-queries/CfgConsistency.ql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import codeql.ruby.controlflow.internal.ControlFlowGraphImpl::Consistency
1+
import codeql.ruby.controlflow.internal.ControlFlowGraphImpl::Consistency as Consistency
2+
import Consistency
23
import codeql.ruby.AST
34
import codeql.ruby.CFG
45
import codeql.ruby.controlflow.internal.Completion
@@ -19,3 +20,14 @@ query predicate nonPostOrderExpr(Expr e, string cls) {
1920
c instanceof NormalCompletion
2021
)
2122
}
23+
24+
query predicate scopeNoFirst(CfgScope scope) {
25+
Consistency::scopeNoFirst(scope) and
26+
not scope = any(StmtSequence seq | not exists(seq.getAStmt())) and
27+
not scope =
28+
any(Callable c |
29+
not exists(c.getAParameter()) and
30+
not c.(BodyStmt).hasEnsure() and
31+
not exists(c.(BodyStmt).getARescue())
32+
)
33+
}

rust/ql/consistency-queries/CfgConsistency.ql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import rust
2-
import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency
2+
import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency as Consistency
3+
import Consistency
4+
import codeql.rust.controlflow.ControlFlowGraph
35
import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
46
import codeql.rust.controlflow.internal.Completion
57

@@ -17,3 +19,9 @@ query predicate nonPostOrderExpr(Expr e, string cls) {
1719
c instanceof NormalCompletion
1820
)
1921
}
22+
23+
query predicate scopeNoFirst(CfgScope scope) {
24+
Consistency::scopeNoFirst(scope) and
25+
not scope = any(Function f | not exists(f.getBody())) and
26+
not scope = any(ClosureExpr c | not exists(c.getBody()))
27+
}

rust/ql/test/library-tests/controlflow/Cfg.ql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @id rust/controlflow/cfg
3-
*/
4-
51
import rust
62
import codeql.rust.controlflow.ControlFlowGraph
73
import TestUtils

shared/controlflow/codeql/controlflow/Cfg.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,9 +1387,13 @@ module Make<LocationSig Location, InputSig<Location> Input> {
13871387
strictcount(sk.getListOrder()) > 1
13881388
}
13891389

1390+
/** Holds if `n` has multiple textual representations. */
13901391
query predicate multipleToString(Node n, string s) {
13911392
s = strictconcat(n.toString(), ",") and
13921393
strictcount(n.toString()) > 1
13931394
}
1395+
1396+
/** Holds if CFG scope `scope` lacks an initial AST node. */
1397+
query predicate scopeNoFirst(CfgScope scope) { not scopeFirst(scope, _) }
13941398
}
13951399
}

0 commit comments

Comments
 (0)