Skip to content

Commit e77fefa

Browse files
authored
Merge pull request #2295 from jbj/self-comparison-templates
C++: Suppress PointlessSelfComparison.ql on templates
2 parents b0fecbc + 97cc0eb commit e77fefa

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ where
1919
pointlessSelfComparison(cmp) and
2020
not nanTest(cmp) and
2121
not overflowTest(cmp) and
22+
not cmp.isFromTemplateInstantiation(_) and
2223
not exists(MacroInvocation mi |
2324
// cmp is in mi
2425
mi.getAnExpandedElement() = cmp and

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| templates.cpp:17:5:17:25 | ... < ... | Self comparison. |
12
| test.cpp:13:11:13:21 | ... == ... | Self comparison. |
23
| test.cpp:79:11:79:32 | ... == ... | Self comparison. |
34
| test.cpp:83:10:83:15 | ... == ... | Self comparison. |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct C1 {
2+
static const int value = 5;
3+
};
4+
5+
struct C2 {
6+
static const int value = 6;
7+
};
8+
9+
template<typename T1, typename T2>
10+
bool compareValues() {
11+
// Not all instantiations have T1 and T2 equal. Even if that's the case for
12+
// all instantiations in the program, there could still be more such
13+
// instantiations outside.
14+
return
15+
T1::value < T2::value || // GOOD
16+
T1::value < T1::value || // BAD [NOT DETECTED]
17+
C1::value < C1::value ; // BAD
18+
}
19+
20+
bool callCompareValues() {
21+
return compareValues<C1, C2> || compareValues<C1, C1>();
22+
}

0 commit comments

Comments
 (0)