Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eea359

Browse files
committed
C++: Test for PointlessComparison with templates
1 parent 604ff23 commit 2eea359

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
| PointlessComparison.c:129:12:129:16 | ... > ... | Comparison is always false because a <= 3. |
3333
| PointlessComparison.c:197:7:197:11 | ... < ... | Comparison is always false because x >= 0. |
3434
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
35+
| Templates.cpp:3:10:3:24 | ... <= ... | Comparison is always true because param <= 32767. |
36+
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |
37+
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |
38+
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
template<typename T>
2+
bool sometimesPointless(T param) {
3+
return param <= 0xFFFF; // GOOD (FALSE POSITIVE: hypothetical instantiations are okay)
4+
}
5+
6+
template<typename T>
7+
bool alwaysPointless(T param) {
8+
short local = param;
9+
return local <= 0xFFFF; // BAD (in all instantiations)
10+
}
11+
12+
static int caller(int i) {
13+
return
14+
sometimesPointless<short>(i) ||
15+
alwaysPointless<short>(i) ||
16+
alwaysPointless<int>(i);
17+
}

0 commit comments

Comments
 (0)