Skip to content

Commit d3666c3

Browse files
committed
C++: Exclude comparisons from enum constants in `cpp/constant-comparison
1 parent 03fe688 commit d3666c3

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ import UnsignedGEZero
2525
//
2626
// So to reduce the number of false positives, we do not report a result if
2727
// the comparison is in a macro expansion. Similarly for template
28-
// instantiations, static asserts, non-type template arguments, and constexprs.
28+
// instantiations, static asserts, non-type template arguments, enum constants,
29+
// and constexprs.
2930
from ComparisonOperation cmp, SmallSide ss, float left, float right, boolean value, string reason
3031
where
3132
not cmp.isInMacroExpansion() and
3233
not cmp.isFromTemplateInstantiation(_) and
3334
not exists(StaticAssert s | s.getCondition() = cmp.getParent*()) and
3435
not exists(Declaration d | d.getATemplateArgument() = cmp.getParent*()) and
3536
not exists(Variable v | v.isConstexpr() | v.getInitializer().getExpr() = cmp.getParent*()) and
37+
not exists(EnumConstant e | e.getInitializer().getExpr() = cmp.getParent*()) and
3638
not functionContainsDisabledCode(cmp.getEnclosingFunction()) and
3739
reachablePointlessComparison(cmp, left, right, value, ss) and
3840
// a comparison between an enum and zero is always valid because whether

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@
5050
| PointlessComparison.cpp:43:6:43:29 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327.5. |
5151
| PointlessComparison.cpp:44:6:44:28 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327.5. |
5252
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
53-
| RegressionTests.cpp:165:9:165:33 | ... > ... | Comparison is always true because ... * ... >= 64. |
5453
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/RegressionTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ struct A_Struct {
162162
};
163163

164164
enum E {
165-
E_e = sizeof(A_Struct) * 8 > 50 // GOOD [FALSE POSITIVE]
165+
E_e = sizeof(A_Struct) * 8 > 50 // GOOD
166166
};

0 commit comments

Comments
 (0)