Skip to content

Commit ae5b58c

Browse files
committed
C++: Exclude more comparisons from cpp/constant-comparison
1 parent 0541dc2 commit ae5b58c

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ 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.
28+
// instantiations, static asserts, non-type template arguments, and constexprs.
2929
from ComparisonOperation cmp, SmallSide ss, float left, float right, boolean value, string reason
3030
where
3131
not cmp.isInMacroExpansion() and
3232
not cmp.isFromTemplateInstantiation(_) and
33+
not exists(StaticAssert s | s.getCondition() = cmp.getParent*()) and
34+
not exists(Declaration d | d.getATemplateArgument() = cmp.getParent*()) and
35+
not exists(Variable v | v.isConstexpr() | v.getInitializer().getExpr() = cmp.getParent*()) and
3336
not functionContainsDisabledCode(cmp.getEnclosingFunction()) and
3437
reachablePointlessComparison(cmp, left, right, value, ss) and
3538
// 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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +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:131:17:131:25 | ... < ... | Comparison is always true because 43 <= ... + .... |
54-
| RegressionTests.cpp:135:26:135:52 | ... < ... | Comparison is always false because 16 >= ... * .... |
55-
| RegressionTests.cpp:140:50:140:61 | ... < ... | Comparison is always false because ... * ... >= 84. |
56-
| RegressionTests.cpp:154:21:154:31 | ... < ... | Comparison is always false because ... - ... >= 34. |
57-
| RegressionTests.cpp:154:21:154:31 | ... < ... | Comparison is always false because ... - ... >= 38. |
5853
| 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@ void testTempObject() {
128128
void staticAssert() {
129129
static const int a = 42;
130130
static const int b = 43;
131-
static_assert(a < b + 0, ""); // GOOD [FALSE POSITIVE]
131+
static_assert(a < b + 0, ""); // GOOD
132132
}
133133

134134
constexpr int global_1 = 42;
135-
constexpr int global_2 = global_1 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD [FALSE POSITIVE]
135+
constexpr int global_2 = global_1 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
136136

137137
static const int global_3 = 42;
138138
static const int global_4 = global_3 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
139139

140140
template<unsigned int p, unsigned int n, bool = ((2u * n) < p)>
141-
struct templateCompare : public templateCompare<p, 2u * n> // GOOD [FALSE POSITIVE]
141+
struct templateCompare : public templateCompare<p, 2u * n> // GOOD
142142
{ };
143143

144144
template< unsigned int p, unsigned int n>
@@ -151,7 +151,7 @@ unsigned int templateCompare_x = templateCompare<42, 42>::v;
151151

152152
template<int n>
153153
struct someType {
154-
typedef someType<((n - 4) < 0 ? 0 : n - 4)> b; // GOOD [FALSE POSITIVE]
154+
typedef someType<((n - 4) < 0 ? 0 : n - 4)> b; // GOOD
155155
};
156156

157157
someType<42>::b someType_x;

0 commit comments

Comments
 (0)