Skip to content

Commit 5016c64

Browse files
authored
Merge pull request github#5859 from MathiasVP/fix-fp-in-comparison-with-wider-type
C++: Fix false positive in `cpp/comparison-with-wider-type`
2 parents 0afe22d + d55db83 commit 5016c64

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The 'Comparison with wider type' (cpp/comparison-with-wider-type) query has been improved to produce fewer false positives.

cpp/ql/src/Security/CWE/CWE-190/ComparisonWithWiderType.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ where
4949
small = rel.getLesserOperand() and
5050
large = rel.getGreaterOperand() and
5151
rel = l.getCondition().getAChild*() and
52-
upperBound(large).log2() > getComparisonSize(small) * 8 and
52+
forall(Expr conv | conv = large.getConversion*() |
53+
upperBound(conv).log2() > getComparisonSize(small) * 8
54+
) and
5355
// Ignore cases where the smaller type is int or larger
5456
// These are still bugs, but you should need a very large string or array to
5557
// trigger them. We will want to disable this for some applications, but it's
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void test_issue_5850(unsigned char small, unsigned int large1) {
2+
for(; small < static_cast<unsigned char>(large1 - 1); small++) { } // GOOD
3+
}
4+
5+
void test_widening(unsigned char small, char large) {
6+
for(; small < static_cast<unsigned int>(static_cast<short>(large) - 1); small++) { } // GOOD
7+
}

0 commit comments

Comments
 (0)