Skip to content

Commit 1203c73

Browse files
authored
Merge pull request github#1261 from geoffw0/autoformat
CPP: Autoformat the Critical queries directory
2 parents 0f10bdc + 09ce955 commit 1203c73

33 files changed

+654
-618
lines changed

cpp/ql/src/Critical/DeadCodeCondition.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ predicate choice(LocalScopeVariable v, Stmt branch, string value) {
2626
exists(AnalysedExpr e |
2727
testAndBranch(e, branch) and
2828
(
29-
(e.getNullSuccessor(v) = branch and value = "null")
29+
e.getNullSuccessor(v) = branch and value = "null"
3030
or
31-
(e.getNonNullSuccessor(v) = branch and value = "non-null")
31+
e.getNonNullSuccessor(v) = branch and value = "non-null"
3232
)
3333
)
3434
}
@@ -60,9 +60,9 @@ where
6060
exists(cond.getNullSuccessor(v)) and
6161
not addressLeak(v, branch.getChildStmt*()) and
6262
(
63-
(cond.isNullCheck(v) and test = "null")
63+
cond.isNullCheck(v) and test = "null"
6464
or
65-
(cond.isValidCheck(v) and test = "non-null")
65+
cond.isValidCheck(v) and test = "non-null"
6666
) and
6767
(if context = test then testresult = "succeed" else testresult = "fail")
6868
select cond,

cpp/ql/src/Critical/DeadCodeFunction.ql

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@
77
* @tags maintainability
88
* external/cwe/cwe-561
99
*/
10+
1011
import cpp
1112

12-
predicate limitedScope(Function f)
13-
{
14-
(f.isStatic() and not (f instanceof MemberFunction))
13+
predicate limitedScope(Function f) {
14+
f.isStatic() and not f instanceof MemberFunction
1515
or
1616
f.(MemberFunction).isPrivate()
1717
}
1818

19-
predicate uncalled(Function f)
20-
{
21-
limitedScope(f)
22-
and not exists(Function g |
23-
g = f or g = f.(VirtualFunction).getAnOverriddenFunction+() |
24-
exists(g.getACallToThisFunction()) or
25-
exists(FunctionAccess fa | fa.getTarget() = g))
19+
predicate uncalled(Function f) {
20+
limitedScope(f) and
21+
not exists(Function g | g = f or g = f.(VirtualFunction).getAnOverriddenFunction+() |
22+
exists(g.getACallToThisFunction()) or
23+
exists(FunctionAccess fa | fa.getTarget() = g)
24+
)
2625
}
2726

2827
from Function f
29-
where uncalled(f)
30-
and forall(Function instance | f.(TemplateFunction).getAnInstantiation() = instance | uncalled(instance))
28+
where
29+
uncalled(f) and
30+
forall(Function instance | f.(TemplateFunction).getAnInstantiation() = instance |
31+
uncalled(instance)
32+
) and
3133
// tweaks for good results:
32-
and exists(f.getBlock())
33-
and not(f instanceof Constructor or f instanceof Destructor or f.hasName("operator="))
34+
exists(f.getBlock()) and
35+
not (f instanceof Constructor or f instanceof Destructor or f.hasName("operator="))
3436
select f, "Dead Code: this function is never called."

cpp/ql/src/Critical/DeadCodeGoto.ql

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Dead code due to goto or break statement
3-
* @description A goto or break statement is followed by unreachable code.
3+
* @description A goto or break statement is followed by unreachable code.
44
* @kind problem
55
* @problem.severity warning
66
* @precision high
@@ -21,16 +21,17 @@ Stmt getNextRealStmt(Block b, int i) {
2121
}
2222

2323
from JumpStmt js, Block b, int i, Stmt s
24-
where b.getStmt(i) = js
25-
and s = getNextRealStmt(b, i)
24+
where
25+
b.getStmt(i) = js and
26+
s = getNextRealStmt(b, i) and
2627
// the next statement isn't jumped to
27-
and not s instanceof LabelStmt
28-
and not s instanceof SwitchCase
28+
not s instanceof LabelStmt and
29+
not s instanceof SwitchCase and
2930
// the next statement isn't breaking out of a switch
30-
and not s.(BreakStmt).getBreakable() instanceof SwitchStmt
31+
not s.(BreakStmt).getBreakable() instanceof SwitchStmt and
3132
// the next statement isn't a loop that can be jumped into
32-
and not exists (LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls)
33-
and not exists (SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc)
33+
not exists(LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls) and
34+
not exists(SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc) and
3435
// no preprocessor logic applies
35-
and not functionContainsPreprocCode(js.getEnclosingFunction())
36+
not functionContainsPreprocCode(js.getEnclosingFunction())
3637
select js, "This statement makes $@ unreachable.", s, s.toString()

cpp/ql/src/Critical/DescriptorMayNotBeClosed.ql

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,53 @@
88
* security
99
* external/cwe/cwe-775
1010
*/
11+
1112
import semmle.code.cpp.pointsto.PointsTo
1213
import Negativity
1314

14-
predicate closeCall(FunctionCall fc, Variable v)
15-
{
16-
(fc.getTarget().hasQualifiedName("close") and v.getAnAccess() = fc.getArgument(0))
15+
predicate closeCall(FunctionCall fc, Variable v) {
16+
fc.getTarget().hasQualifiedName("close") and v.getAnAccess() = fc.getArgument(0)
1717
or
1818
exists(FunctionCall midcall, Function mid, int arg |
1919
fc.getArgument(arg) = v.getAnAccess() and
2020
fc.getTarget() = mid and
2121
midcall.getEnclosingFunction() = mid and
22-
closeCall(midcall, mid.getParameter(arg)))
22+
closeCall(midcall, mid.getParameter(arg))
23+
)
2324
}
2425

25-
predicate openDefinition(LocalScopeVariable v, ControlFlowNode def)
26-
{
27-
exists(Expr expr |
28-
exprDefinition(v, def, expr) and allocateDescriptorCall(expr))
26+
predicate openDefinition(LocalScopeVariable v, ControlFlowNode def) {
27+
exists(Expr expr | exprDefinition(v, def, expr) and allocateDescriptorCall(expr))
2928
}
3029

31-
predicate openReaches(ControlFlowNode def, ControlFlowNode node)
32-
{
33-
exists(LocalScopeVariable v |
34-
openDefinition(v, def) and node = def.getASuccessor())
30+
predicate openReaches(ControlFlowNode def, ControlFlowNode node) {
31+
exists(LocalScopeVariable v | openDefinition(v, def) and node = def.getASuccessor())
3532
or
3633
exists(LocalScopeVariable v, ControlFlowNode mid |
3734
openDefinition(v, def) and
3835
openReaches(def, mid) and
39-
not(errorSuccessor(v, mid)) and
40-
not(closeCall(mid, v)) and
41-
not(assignedToFieldOrGlobal(v, mid)) and
42-
node = mid.getASuccessor())
36+
not errorSuccessor(v, mid) and
37+
not closeCall(mid, v) and
38+
not assignedToFieldOrGlobal(v, mid) and
39+
node = mid.getASuccessor()
40+
)
4341
}
4442

45-
predicate assignedToFieldOrGlobal(LocalScopeVariable v, Assignment assign)
46-
{
43+
predicate assignedToFieldOrGlobal(LocalScopeVariable v, Assignment assign) {
4744
exists(Variable external |
4845
assign.getRValue() = v.getAnAccess() and
4946
assign.getLValue().(VariableAccess).getTarget() = external and
50-
(external instanceof Field or external instanceof GlobalVariable))
47+
(external instanceof Field or external instanceof GlobalVariable)
48+
)
5149
}
5250

5351
from LocalScopeVariable v, ControlFlowNode def, ReturnStmt ret
54-
where openDefinition(v, def)
55-
and openReaches(def, ret)
56-
and checkedSuccess(v, ret)
57-
and not(ret.getExpr().getAChild*() = v.getAnAccess())
58-
and exists(ReturnStmt other | other.getExpr() = v.getAnAccess())
52+
where
53+
openDefinition(v, def) and
54+
openReaches(def, ret) and
55+
checkedSuccess(v, ret) and
56+
not ret.getExpr().getAChild*() = v.getAnAccess() and
57+
exists(ReturnStmt other | other.getExpr() = v.getAnAccess())
5958
select ret,
6059
"Descriptor assigned to '" + v.getName().toString() + "' (line " +
61-
def.getLocation().getStartLine().toString() + ") may not be closed."
60+
def.getLocation().getStartLine().toString() + ") may not be closed."

cpp/ql/src/Critical/DescriptorNeverClosed.ql

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88
* security
99
* external/cwe/cwe-775
1010
*/
11+
1112
import semmle.code.cpp.pointsto.PointsTo
1213

13-
predicate closed(Expr e)
14-
{
14+
predicate closed(Expr e) {
1515
exists(FunctionCall fc |
1616
fc.getTarget().hasQualifiedName("close") and
17-
fc.getArgument(0) = e)
17+
fc.getArgument(0) = e
18+
)
1819
}
1920

20-
class ClosedExpr extends PointsToExpr
21-
{
21+
class ClosedExpr extends PointsToExpr {
2222
ClosedExpr() { closed(this) }
23+
2324
override predicate interesting() { closed(this) }
2425
}
2526

2627
from Expr alloc
27-
where allocateDescriptorCall(alloc)
28-
and not exists(ClosedExpr closed | closed.pointsTo() = alloc)
28+
where
29+
allocateDescriptorCall(alloc) and
30+
not exists(ClosedExpr closed | closed.pointsTo() = alloc)
2931
select alloc, "This file descriptor is never closed"

cpp/ql/src/Critical/FileClosed.qll

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import semmle.code.cpp.pointsto.PointsTo
22

33
predicate closed(Expr e) {
4-
fcloseCall(_, e) or
5-
exists(ExprCall c |
6-
// cautiously assume that any ExprCall could be a call to fclose.
7-
c.getAnArgument() = e
8-
)
4+
fcloseCall(_, e) or
5+
exists(ExprCall c |
6+
// cautiously assume that any ExprCall could be a call to fclose.
7+
c.getAnArgument() = e
8+
)
99
}
1010

1111
class ClosedExpr extends PointsToExpr {
12-
ClosedExpr() { closed(this) }
13-
override predicate interesting() { closed(this) }
14-
}
12+
ClosedExpr() { closed(this) }
1513

16-
predicate fopenCallMayBeClosed(FunctionCall fc) {
17-
fopenCall(fc) and anythingPointsTo(fc)
14+
override predicate interesting() { closed(this) }
1815
}
16+
17+
predicate fopenCallMayBeClosed(FunctionCall fc) { fopenCall(fc) and anythingPointsTo(fc) }

0 commit comments

Comments
 (0)