1
+ /**
2
+ * Provides predicates for reasoning about when the value of an expression is
3
+ * guarded by an operation such as `<`, which confines its range.
4
+ */
5
+
1
6
import cpp
2
7
import semmle.code.cpp.controlflow.Dominance
3
8
4
- /*
5
- * Guarding
9
+ /**
10
+ * Holds if the value of `use` is guarded using `abs`.
6
11
*/
7
-
8
- /** is the size of this use guarded using 'abs'? */
9
12
predicate guardedAbs ( Operation e , Expr use ) {
10
13
exists ( FunctionCall fc | fc .getTarget ( ) .getName ( ) = "abs" |
11
14
fc .getArgument ( 0 ) .getAChild * ( ) = use and
12
15
guardedLesser ( e , fc )
13
16
)
14
17
}
15
18
16
- /** This is `BasicBlock.getNode`, restricted to `Stmt` for performance. */
19
+ /**
20
+ * Gets the result of `BasicBlock.getNode`, but only on a `Stmt` (for
21
+ * performance).
22
+ */
17
23
pragma [ noinline]
18
24
private int getStmtIndexInBlock ( BasicBlock block , Stmt stmt ) { block .getNode ( result ) = stmt }
19
25
@@ -30,7 +36,9 @@ private predicate stmtDominates(Stmt dominator, Stmt dominated) {
30
36
bbStrictlyDominates ( dominator .getBasicBlock ( ) , dominated .getBasicBlock ( ) )
31
37
}
32
38
33
- /** is the size of this use guarded to be less than something? */
39
+ /**
40
+ * Holds if the value of `use` is guarded to be less than something.
41
+ */
34
42
pragma [ nomagic]
35
43
predicate guardedLesser ( Operation e , Expr use ) {
36
44
exists ( IfStmt c , RelationalOperation guard |
@@ -54,7 +62,9 @@ predicate guardedLesser(Operation e, Expr use) {
54
62
guardedAbs ( e , use )
55
63
}
56
64
57
- /** is the size of this use guarded to be greater than something? */
65
+ /**
66
+ * Holds if the value of `use` is guarded to be greater than something.
67
+ */
58
68
pragma [ nomagic]
59
69
predicate guardedGreater ( Operation e , Expr use ) {
60
70
exists ( IfStmt c , RelationalOperation guard |
@@ -78,10 +88,14 @@ predicate guardedGreater(Operation e, Expr use) {
78
88
guardedAbs ( e , use )
79
89
}
80
90
81
- /** a use of a given variable */
91
+ /**
92
+ * Gets a use of a given variable `v`.
93
+ */
82
94
VariableAccess varUse ( LocalScopeVariable v ) { result = v .getAnAccess ( ) }
83
95
84
- /** is e not guarded against overflow by use? */
96
+ /**
97
+ * Holds if `e` is not guarded against overflow by `use`.
98
+ */
85
99
predicate missingGuardAgainstOverflow ( Operation e , VariableAccess use ) {
86
100
use = e .getAnOperand ( ) and
87
101
exists ( LocalScopeVariable v | use .getTarget ( ) = v |
@@ -100,7 +114,9 @@ predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
100
114
)
101
115
}
102
116
103
- /** is e not guarded against underflow by use? */
117
+ /**
118
+ * Holds if `e` is not guarded against underflow by `use`.
119
+ */
104
120
predicate missingGuardAgainstUnderflow ( Operation e , VariableAccess use ) {
105
121
use = e .getAnOperand ( ) and
106
122
exists ( LocalScopeVariable v | use .getTarget ( ) = v |
0 commit comments