Merge pull request #303 from jbj/UnsignedGEZero-templates

C++: Suppress UnsignedGEZero.ql in template instantiations
This commit is contained in:
Geoffrey White
2018-10-12 09:43:48 +01:00
committed by GitHub
3 changed files with 19 additions and 0 deletions

View File

@@ -50,5 +50,6 @@ predicate unsignedGEZero(UnsignedGEZero ugez, string msg) {
ugez.getLocation().getStartLine() = mi.getLocation().getStartLine() and
ugez.getLocation().getStartColumn() = mi.getLocation().getStartColumn()
) and
not ugez.isFromTemplateInstantiation(_) and
msg = "Pointless comparison of unsigned value to zero."
}

View File

@@ -0,0 +1,17 @@
template<typename T>
bool sometimesPointless(T param) {
return param >= 0; // GOOD (FALSE POSITIVE: hypothetical instantiations are okay)
}
template<typename T>
bool alwaysPointless(T param) {
unsigned int local = param;
return local >= 0; // BAD (in all instantiations)
}
static int caller(int i) {
return
sometimesPointless<unsigned int>(i) ||
alwaysPointless<unsigned int>(i) ||
alwaysPointless<int>(i);
}

View File

@@ -1,3 +1,4 @@
| Templates.cpp:9:10:9:19 | ... >= ... | Pointless comparison of unsigned value to zero. |
| UnsignedGEZero.c:40:6:40:12 | ... >= ... | Pointless comparison of unsigned value to zero. |
| UnsignedGEZero.c:48:6:48:15 | ... >= ... | Pointless comparison of unsigned value to zero. |
| UnsignedGEZero.c:54:6:54:12 | ... >= ... | Pointless comparison of unsigned value to zero. |