Skip to content

Commit 2cee756

Browse files
committed
C++: Support the mirror case with <=.
1 parent 3c96b09 commit 2cee756

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ class ConstantZero extends Expr {
1919
* Holds if `candidate` is an expression such that if it's unsigned then we
2020
* want an alert at `ge`.
2121
*/
22-
private predicate lookForUnsignedAt(GEExpr ge, Expr candidate) {
23-
// Base case: `candidate >= 0`
22+
private predicate lookForUnsignedAt(RelationalOperation ge, Expr candidate) {
23+
// Base case: `candidate >= 0` (or `0 <= candidate`)
24+
(
25+
ge instanceof GEExpr or
26+
ge instanceof LEExpr
27+
) and
2428
ge.getLesserOperand() instanceof ConstantZero and
2529
candidate = ge.getGreaterOperand().getFullyConverted() and
2630
// left/greater operand was a signed or unsigned IntegralType before conversions
@@ -37,7 +41,7 @@ private predicate lookForUnsignedAt(GEExpr ge, Expr candidate) {
3741
)
3842
}
3943

40-
class UnsignedGEZero extends GEExpr {
44+
class UnsignedGEZero extends ComparisonOperation {
4145
UnsignedGEZero() {
4246
exists(Expr ue |
4347
lookForUnsignedAt(this, ue) and

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void myFunction() {
133133

134134
if (ui <= 0) {
135135
}
136-
if (0 <= ui) { // violation [NOT DETECTED]
136+
if (0 <= ui) { // violation
137137
}
138138
if (0 < ui) {
139139
}

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void myFunction() {
133133

134134
if (ui <= 0) {
135135
}
136-
if (0 <= ui) { // violation [NOT DETECTED]
136+
if (0 <= ui) { // violation
137137
}
138138
if (0 < ui) {
139139
}

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| UnsignedGEZero.c:122:6:122:40 | ... >= ... | Pointless comparison of unsigned value to zero. |
2121
| UnsignedGEZero.c:127:6:127:51 | ... >= ... | Pointless comparison of unsigned value to zero. |
2222
| UnsignedGEZero.c:131:6:131:52 | ... >= ... | Pointless comparison of unsigned value to zero. |
23+
| UnsignedGEZero.c:136:6:136:12 | ... <= ... | Pointless comparison of unsigned value to zero. |
2324
| UnsignedGEZero.cpp:40:6:40:12 | ... >= ... | Pointless comparison of unsigned value to zero. |
2425
| UnsignedGEZero.cpp:48:6:48:15 | ... >= ... | Pointless comparison of unsigned value to zero. |
2526
| UnsignedGEZero.cpp:54:6:54:12 | ... >= ... | Pointless comparison of unsigned value to zero. |
@@ -41,3 +42,4 @@
4142
| UnsignedGEZero.cpp:122:6:122:40 | ... >= ... | Pointless comparison of unsigned value to zero. |
4243
| UnsignedGEZero.cpp:127:6:127:51 | ... >= ... | Pointless comparison of unsigned value to zero. |
4344
| UnsignedGEZero.cpp:131:6:131:52 | ... >= ... | Pointless comparison of unsigned value to zero. |
45+
| UnsignedGEZero.cpp:136:6:136:12 | ... <= ... | Pointless comparison of unsigned value to zero. |

0 commit comments

Comments
 (0)