|
7 | 7 | * @tags reliability |
8 | 8 | * external/cwe/cwe-561 |
9 | 9 | */ |
| 10 | + |
10 | 11 | import cpp |
11 | 12 |
|
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 | + ) |
16 | 18 | 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 | + ) |
19 | 23 | } |
20 | 24 |
|
21 | | -predicate choice(LocalScopeVariable v, Stmt branch, string value) |
22 | | -{ |
| 25 | +predicate choice(LocalScopeVariable v, Stmt branch, string value) { |
23 | 26 | exists(AnalysedExpr e | |
24 | 27 | testAndBranch(e, branch) and |
25 | 28 | ( |
26 | 29 | (e.getNullSuccessor(v) = branch and value = "null") |
27 | 30 | or |
28 | 31 | (e.getNonNullSuccessor(v) = branch and value = "non-null") |
29 | | - )) |
| 32 | + ) |
| 33 | + ) |
30 | 34 | } |
31 | 35 |
|
32 | | - |
33 | | -predicate guarded(LocalScopeVariable v, Stmt loopstart, AnalysedExpr child) |
34 | | -{ |
| 36 | +predicate guarded(LocalScopeVariable v, Stmt loopstart, AnalysedExpr child) { |
35 | 37 | choice(v, loopstart, _) and |
36 | 38 | loopstart.getChildStmt*() = child.getEnclosingStmt() and |
37 | 39 | (definition(v, child) or exists(child.getNullSuccessor(v))) |
38 | 40 | } |
39 | 41 |
|
40 | | -predicate addressLeak(Variable v, Stmt leak) |
41 | | -{ |
| 42 | +predicate addressLeak(Variable v, Stmt leak) { |
42 | 43 | exists(VariableAccess access | |
43 | 44 | v.getAnAccess() = access and |
44 | 45 | access.getEnclosingStmt() = leak and |
45 | | - access.isAddressOfAccess()) |
| 46 | + access.isAddressOfAccess() |
| 47 | + ) |
46 | 48 | } |
47 | 49 |
|
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 + "." |
0 commit comments