Skip to content

Commit 346bc1a

Browse files
committed
CPP: Autoformat some code from Critical.
1 parent b7febb0 commit 346bc1a

File tree

4 files changed

+113
-84
lines changed

4 files changed

+113
-84
lines changed

cpp/ql/src/Critical/DeadCodeCondition.ql

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,64 @@
77
* @tags reliability
88
* external/cwe/cwe-561
99
*/
10+
1011
import cpp
1112

12-
predicate testAndBranch(Expr e, Stmt branch)
13-
{
14-
exists(IfStmt ifstmt | ifstmt.getCondition() = e and
15-
(ifstmt.getThen() = branch or ifstmt.getElse() = branch))
13+
predicate testAndBranch(Expr e, Stmt branch) {
14+
exists(IfStmt ifstmt |
15+
ifstmt.getCondition() = e and
16+
(ifstmt.getThen() = branch or ifstmt.getElse() = branch)
17+
)
1618
or
17-
exists(WhileStmt while | while.getCondition() = e and
18-
while.getStmt() = branch)
19+
exists(WhileStmt while |
20+
while.getCondition() = e and
21+
while.getStmt() = branch
22+
)
1923
}
2024

21-
predicate choice(LocalScopeVariable v, Stmt branch, string value)
22-
{
25+
predicate choice(LocalScopeVariable v, Stmt branch, string value) {
2326
exists(AnalysedExpr e |
2427
testAndBranch(e, branch) and
2528
(
2629
(e.getNullSuccessor(v) = branch and value = "null")
2730
or
2831
(e.getNonNullSuccessor(v) = branch and value = "non-null")
29-
))
32+
)
33+
)
3034
}
3135

32-
33-
predicate guarded(LocalScopeVariable v, Stmt loopstart, AnalysedExpr child)
34-
{
36+
predicate guarded(LocalScopeVariable v, Stmt loopstart, AnalysedExpr child) {
3537
choice(v, loopstart, _) and
3638
loopstart.getChildStmt*() = child.getEnclosingStmt() and
3739
(definition(v, child) or exists(child.getNullSuccessor(v)))
3840
}
3941

40-
predicate addressLeak(Variable v, Stmt leak)
41-
{
42+
predicate addressLeak(Variable v, Stmt leak) {
4243
exists(VariableAccess access |
4344
v.getAnAccess() = access and
4445
access.getEnclosingStmt() = leak and
45-
access.isAddressOfAccess())
46+
access.isAddressOfAccess()
47+
)
4648
}
4749

48-
from LocalScopeVariable v, Stmt branch, AnalysedExpr cond, string context, string test, string testresult
49-
where choice(v, branch, context)
50-
and forall(ControlFlowNode def | definition(v, def) and definitionReaches(def, cond) | not guarded(v, branch, def))
51-
and not cond.isDef(v)
52-
and guarded(v, branch, cond)
53-
and exists(cond.getNullSuccessor(v))
54-
and not addressLeak(v, branch.getChildStmt*())
55-
and ((cond.isNullCheck(v) and test = "null") or (cond.isValidCheck(v) and test = "non-null"))
56-
and (if context = test then testresult = "succeed" else testresult = "fail")
57-
select cond, "Variable '" + v.getName() + "' is always " + context + " here, this check will always " + testresult + "."
50+
from
51+
LocalScopeVariable v, Stmt branch, AnalysedExpr cond, string context, string test,
52+
string testresult
53+
where
54+
choice(v, branch, context) and
55+
forall(ControlFlowNode def | definition(v, def) and definitionReaches(def, cond) |
56+
not guarded(v, branch, def)
57+
) and
58+
not cond.isDef(v) and
59+
guarded(v, branch, cond) and
60+
exists(cond.getNullSuccessor(v)) and
61+
not addressLeak(v, branch.getChildStmt*()) and
62+
(
63+
(cond.isNullCheck(v) and test = "null")
64+
or
65+
(cond.isValidCheck(v) and test = "non-null")
66+
) and
67+
(if context = test then testresult = "succeed" else testresult = "fail")
68+
select cond,
69+
"Variable '" + v.getName() + "' is always " + context + " here, this check will always " +
70+
testresult + "."

cpp/ql/src/Critical/NotInitialised.ql

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@
77
* @tags reliability
88
* external/cwe/cwe-457
99
*/
10+
1011
import cpp
1112

1213
// See also InitialisationNotRun.ql and GlobalUseBeforeInit.ql
13-
1414
// Holds if s defines variable v (conservative)
1515
predicate defines(ControlFlowNode s, Variable lv) {
1616
exists(VariableAccess va | va = s and va.getTarget() = lv and va.isUsedAsLValue())
1717
}
1818

1919
// Holds if s uses variable v (conservative)
2020
predicate uses(ControlFlowNode s, Variable lv) {
21-
exists(VariableAccess va | va = s and va.getTarget() = lv and va.isRValue()
22-
and not va.getParent+() instanceof SizeofOperator)
21+
exists(VariableAccess va |
22+
va = s and
23+
va.getTarget() = lv and
24+
va.isRValue() and
25+
not va.getParent+() instanceof SizeofOperator
26+
)
2327
}
2428

2529
// Holds if there is a path from the declaration of lv to n such that lv is
2630
// definitely not defined before n
2731
predicate noDefPath(LocalVariable lv, ControlFlowNode n) {
28-
n.(DeclStmt).getADeclaration() = lv and not exists(lv.getInitializer())
29-
or exists(ControlFlowNode p | noDefPath(lv, p) and n = p.getASuccessor() and not defines(p, lv))
32+
n.(DeclStmt).getADeclaration() = lv and not exists(lv.getInitializer())
33+
or
34+
exists(ControlFlowNode p | noDefPath(lv, p) and n = p.getASuccessor() and not defines(p, lv))
3035
}
3136

32-
predicate isAggregateType(Type t) {
33-
t instanceof Class or t instanceof ArrayType
34-
}
37+
predicate isAggregateType(Type t) { t instanceof Class or t instanceof ArrayType }
3538

3639
// Holds if va is a use of a local variable that has not been previously
3740
// defined
@@ -43,7 +46,8 @@ predicate undefinedLocalUse(VariableAccess va) {
4346
not lv.getType().hasName("va_list") and
4447
va = lv.getAnAccess() and
4548
noDefPath(lv, va) and
46-
uses(va, lv))
49+
uses(va, lv)
50+
)
4751
}
4852

4953
// Holds if gv is a potentially uninitialized global variable
@@ -53,7 +57,8 @@ predicate uninitialisedGlobal(GlobalVariable gv) {
5357
va = gv.getAnAccess() and
5458
va.isRValue() and
5559
not gv.hasInitializer() and
56-
not gv.hasSpecifier("extern"))
60+
not gv.hasSpecifier("extern")
61+
)
5762
}
5863

5964
from Element elt

cpp/ql/src/Critical/SizeCheck.ql

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,61 @@
1111
* external/cwe/cwe-131
1212
* external/cwe/cwe-122
1313
*/
14+
1415
import cpp
1516

16-
class Allocation extends FunctionCall
17-
{
17+
class Allocation extends FunctionCall {
1818
Allocation() {
1919
exists(string name |
2020
this.getTarget().hasQualifiedName(name) and
21-
(name = "malloc" or name = "calloc" or name = "realloc"))
21+
(name = "malloc" or name = "calloc" or name = "realloc")
22+
)
2223
}
2324

2425
string getName() { result = this.getTarget().getQualifiedName() }
2526

2627
int getSize() {
27-
(this.getName() = "malloc" and
28-
this.getArgument(0).getValue().toInt() = result)
28+
(
29+
this.getName() = "malloc" and
30+
this.getArgument(0).getValue().toInt() = result
31+
)
2932
or
30-
(this.getName() = "realloc" and
31-
this.getArgument(1).getValue().toInt() = result)
33+
(
34+
this.getName() = "realloc" and
35+
this.getArgument(1).getValue().toInt() = result
36+
)
3237
or
33-
(this.getName() = "calloc" and
34-
result =
35-
this.getArgument(0).getValue().toInt() *
36-
this.getArgument(1).getValue().toInt())
38+
(
39+
this.getName() = "calloc" and
40+
result = this.getArgument(0).getValue().toInt() * this.getArgument(1).getValue().toInt()
41+
)
3742
}
3843
}
3944

40-
predicate baseType(Allocation alloc, Type base)
41-
{
45+
predicate baseType(Allocation alloc, Type base) {
4246
exists(PointerType pointer |
4347
pointer.getBaseType() = base and
4448
(
4549
exists(AssignExpr assign |
46-
assign.getRValue() = alloc and assign.getLValue().getType() = pointer)
50+
assign.getRValue() = alloc and assign.getLValue().getType() = pointer
51+
)
4752
or
48-
exists(Variable v |
49-
v.getInitializer().getExpr() = alloc and v.getType() = pointer)
53+
exists(Variable v | v.getInitializer().getExpr() = alloc and v.getType() = pointer)
5054
)
5155
)
5256
}
5357

54-
predicate decideOnSize(Type t, int size)
55-
{
58+
predicate decideOnSize(Type t, int size) {
5659
// If the codebase has more than one type with the same name, it can have more than one size.
5760
size = min(t.getSize())
5861
}
5962

6063
from Allocation alloc, Type base, int basesize, int allocated
61-
where baseType(alloc, base)
62-
and allocated = alloc.getSize()
63-
and decideOnSize(base, basesize)
64-
and basesize > allocated
65-
select alloc, "Type '" + base.getName() + "' is " + basesize.toString() +
66-
" bytes, but only " + allocated.toString() + " bytes are allocated."
64+
where
65+
baseType(alloc, base) and
66+
allocated = alloc.getSize() and
67+
decideOnSize(base, basesize) and
68+
basesize > allocated
69+
select alloc,
70+
"Type '" + base.getName() + "' is " + basesize.toString() + " bytes, but only " +
71+
allocated.toString() + " bytes are allocated."

cpp/ql/src/Critical/SizeCheck2.ql

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,60 @@
1111
* external/cwe/cwe-131
1212
* external/cwe/cwe-122
1313
*/
14+
1415
import cpp
1516

16-
class Allocation extends FunctionCall
17-
{
17+
class Allocation extends FunctionCall {
1818
Allocation() {
1919
exists(string name |
2020
this.getTarget().hasQualifiedName(name) and
21-
(name = "malloc" or name = "calloc" or name = "realloc"))
21+
(name = "malloc" or name = "calloc" or name = "realloc")
22+
)
2223
}
2324

2425
string getName() { result = this.getTarget().getQualifiedName() }
2526

2627
int getSize() {
27-
(this.getName() = "malloc" and
28-
this.getArgument(0).getValue().toInt() = result)
28+
(
29+
this.getName() = "malloc" and
30+
this.getArgument(0).getValue().toInt() = result
31+
)
2932
or
30-
(this.getName() = "realloc" and
31-
this.getArgument(1).getValue().toInt() = result)
33+
(
34+
this.getName() = "realloc" and
35+
this.getArgument(1).getValue().toInt() = result
36+
)
3237
or
33-
(this.getName() = "calloc" and
34-
result =
35-
this.getArgument(0).getValue().toInt() *
36-
this.getArgument(1).getValue().toInt())
38+
(
39+
this.getName() = "calloc" and
40+
result = this.getArgument(0).getValue().toInt() * this.getArgument(1).getValue().toInt()
41+
)
3742
}
3843
}
3944

40-
predicate baseType(Allocation alloc, Type base)
41-
{
45+
predicate baseType(Allocation alloc, Type base) {
4246
exists(PointerType pointer |
4347
pointer.getBaseType() = base and
4448
(
4549
exists(AssignExpr assign |
46-
assign.getRValue() = alloc and assign.getLValue().getType() = pointer)
50+
assign.getRValue() = alloc and assign.getLValue().getType() = pointer
51+
)
4752
or
48-
exists(Variable v |
49-
v.getInitializer().getExpr() = alloc and v.getType() = pointer)
53+
exists(Variable v | v.getInitializer().getExpr() = alloc and v.getType() = pointer)
5054
)
5155
)
5256
}
5357

5458
from Allocation alloc, Type base, int basesize, int allocated
55-
where baseType(alloc, base)
56-
and allocated = alloc.getSize()
59+
where
60+
baseType(alloc, base) and
61+
allocated = alloc.getSize() and
5762
// If the codebase has more than one type with the same name, check if any matches
58-
and not exists(int size | base.getSize() = size |
59-
size = 0
60-
or (allocated / size) * size = allocated)
61-
and basesize = min(base.getSize())
62-
select alloc, "Allocated memory (" + allocated.toString() +
63-
" bytes) is not a multiple of the size of '" +
64-
base.getName() + "' (" + basesize.toString() + " bytes)."
63+
not exists(int size | base.getSize() = size |
64+
size = 0 or
65+
(allocated / size) * size = allocated
66+
) and
67+
basesize = min(base.getSize())
68+
select alloc,
69+
"Allocated memory (" + allocated.toString() + " bytes) is not a multiple of the size of '" +
70+
base.getName() + "' (" + basesize.toString() + " bytes)."

0 commit comments

Comments
 (0)