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

Skip to content

Commit 30412ca

Browse files
authored
Merge pull request #276 from jbj/PointlessComparison-templates
Approved by kevinbackhouse
2 parents ae9b492 + 364c9a6 commit 30412ca

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ import UnsignedGEZero
2222
// #define PRINTMSG(val,msg) { if (val >= PRINTLEVEL) printf(msg); }
2323
//
2424
// So to reduce the number of false positives, we do not report a result if
25-
// the comparison is in a macro expansion.
25+
// the comparison is in a macro expansion. Similarly for template
26+
// instantiations.
2627
from
2728
ComparisonOperation cmp, SmallSide ss,
2829
float left, float right, boolean value,
2930
string reason
3031
where
3132
not cmp.isInMacroExpansion() and
33+
not cmp.isFromTemplateInstantiation(_) and
3234
reachablePointlessComparison(cmp, left, right, value, ss) and
3335

3436
// 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
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: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 (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)