Skip to content

Commit 3ecd135

Browse files
committed
C++: Improve isGuarded.
1 parent 59ff3f3 commit 3ecd135

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

cpp/ql/src/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero.ql

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1616
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1717
import semmle.code.cpp.controlflow.Guards
1818

19-
/** Holds if `sub` is guarded by a condition which ensures that `left >= right`. */
19+
/**
20+
* Holds if `sub` is guarded by a condition which ensures that
21+
* `left >= right`.
22+
*/
2023
pragma[noinline]
2124
predicate isGuarded(SubExpr sub, Expr left, Expr right) {
22-
exists(GuardCondition guard |
23-
guard.controls(sub.getBasicBlock(), true) and
24-
guard.ensuresLt(left, right, 0, sub.getBasicBlock(), false)
25+
exists(GuardCondition guard, int k |
26+
guard.controls(sub.getBasicBlock(), _) and
27+
guard.ensuresLt(left, right, k, sub.getBasicBlock(), false) and
28+
k >= 0
2529
)
2630
}
2731

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/UnsignedDifferenceExpressionComparedZero.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
| test.cpp:137:6:137:14 | ... > ... | Unsigned subtraction can never be negative. |
1818
| test.cpp:146:7:146:15 | ... > ... | Unsigned subtraction can never be negative. |
1919
| test.cpp:152:7:152:15 | ... > ... | Unsigned subtraction can never be negative. |
20-
| test.cpp:156:7:156:15 | ... > ... | Unsigned subtraction can never be negative. |
21-
| test.cpp:169:6:169:14 | ... > ... | Unsigned subtraction can never be negative. |
2220
| test.cpp:182:6:182:14 | ... > ... | Unsigned subtraction can never be negative. |
2321
| test.cpp:195:6:195:14 | ... > ... | Unsigned subtraction can never be negative. |
2422
| test.cpp:208:6:208:14 | ... > ... | Unsigned subtraction can never be negative. |

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void test8() {
153153
// ...
154154
}
155155
} else {
156-
if (a - b > 0) { // GOOD (as a > b) [FALSE POSITIVE]
156+
if (a - b > 0) { // GOOD (as a > b)
157157
// ...
158158
}
159159
}
@@ -166,7 +166,7 @@ void test8() {
166166

167167
if (a < b) return;
168168

169-
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
169+
if (a - b > 0) { // GOOD (as a >= b)
170170
// ...
171171
}
172172
}

0 commit comments

Comments
 (0)