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

Skip to content

Commit 364c9a6

Browse files
committed
C++: Suppress pointless compare in template inst.
It still runs on uninstantiated templates because its underlying libraries do. It's not clear whether that leads to other false positives, but that's independent of the change I'm making here.
1 parent 2eea359 commit 364c9a6

3 files changed

Lines changed: 4 additions & 5 deletions

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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +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: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. |
3835
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
template<typename T>
22
bool sometimesPointless(T param) {
3-
return param <= 0xFFFF; // GOOD (FALSE POSITIVE: hypothetical instantiations are okay)
3+
return param <= 0xFFFF; // GOOD (hypothetical instantiations are okay)
44
}
55

66
template<typename T>

0 commit comments

Comments
 (0)