diff --git a/cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll b/cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll index 3c775db031f3..8ee3e9f6fc2a 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll +++ b/cpp/ql/src/Likely Bugs/Arithmetic/UnsignedGEZero.qll @@ -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." } diff --git a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/Templates.cpp b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/Templates.cpp new file mode 100644 index 000000000000..a56f9c88c81d --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/Templates.cpp @@ -0,0 +1,17 @@ +template +bool sometimesPointless(T param) { + return param >= 0; // GOOD (FALSE POSITIVE: hypothetical instantiations are okay) +} + +template +bool alwaysPointless(T param) { + unsigned int local = param; + return local >= 0; // BAD (in all instantiations) +} + +static int caller(int i) { + return + sometimesPointless(i) || + alwaysPointless(i) || + alwaysPointless(i); +} diff --git a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.expected b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.expected index 0e232e688292..bb1087981723 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/UnsignedGEZero/UnsignedGEZero.expected @@ -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. |