Skip to content

Commit 9363d64

Browse files
authored
Merge pull request #7395 from MathiasVP/fix-fp-in-pointless-self-comparison
C++: Fix FP in `cpp/comparison-of-identical-expressions`
2 parents 0e7fdbe + 65c301c commit 9363d64

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ private predicate cancelingSubExprs(ComparisonOperation cmp, VariableAccess a1,
8585
exists(Variable v |
8686
exists(float m | m < 0 and cmpLinearSubVariable(cmp, v, a1, m)) and
8787
exists(float m | m > 0 and cmpLinearSubVariable(cmp, v, a2, m))
88-
)
88+
) and
89+
not any(ClassTemplateInstantiation inst).getATemplateArgument() = cmp.getParent*()
8990
}
9091

9192
from ComparisonOperation cmp, VariableAccess a1, VariableAccess a2

cpp/ql/src/Likely Bugs/Arithmetic/PointlessSelfComparison.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ predicate pointlessSelfComparison(ComparisonOperation cmp) {
2929
not exists(lhs.getQualifier()) and // Avoid structure fields
3030
not exists(rhs.getQualifier()) and // Avoid structure fields
3131
not convertedExprMightOverflow(lhs) and
32-
not convertedExprMightOverflow(rhs)
32+
not convertedExprMightOverflow(rhs) and
33+
// Don't warn if the comparison is part of a template argument.
34+
not any(ClassTemplateInstantiation inst).getATemplateArgument() = cmp.getParent*()
3335
)
3436
}
3537

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/BadAdditionOverflowCheck/templates.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ bool compareValues() {
2020
bool callCompareValues() {
2121
return compareValues<C1, C2> || compareValues<C1, C1>();
2222
}
23+
24+
template <bool C, typename T = void>
25+
struct enable_if {};
26+
27+
template <typename T>
28+
struct enable_if<true, T> { typedef T type; };
29+
30+
template<typename T1, typename T2>
31+
typename enable_if<T1::value <= T2::value, bool>::type constant_comparison() {
32+
return true;
33+
}
34+
35+
struct Value0 {
36+
const static int value = 0;
37+
};
38+
39+
void instantiation_with_pointless_comparison() {
40+
constant_comparison<Value0, Value0>(); // GOOD
41+
}

0 commit comments

Comments
 (0)