Skip to content

Commit c0b6531

Browse files
committed
C++: Fix false positive by restricting _both_ the old (unconverted) expression _and_ all of the conversions.
1 parent c7cd754 commit c0b6531

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ where
4949
small = rel.getLesserOperand() and
5050
large = rel.getGreaterOperand() and
5151
rel = l.getCondition().getAChild*() and
52+
forall(Expr conv | conv = large.getConversion*() |
53+
upperBound(conv).log2() > getComparisonSize(small) * 8
54+
) and
5255
upperBound(large.getFullyConverted()).log2() > getComparisonSize(small) * 8 and
5356
// Ignore cases where the smaller type is int or larger
5457
// These are still bugs, but you should need a very large string or array to

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/ComparisonWithWiderType/ComparisonWithWiderType.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| test3.cpp:6:8:6:71 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type int. | test3.cpp:5:34:5:38 | small | small | test3.cpp:6:42:6:70 | ... - ... | ... - ... |
21
| test.c:4:14:4:18 | ... < ... | Comparison between $@ of type char and $@ of wider type int. | test.c:3:7:3:7 | c | c | test.c:2:17:2:17 | x | x |
32
| test.c:9:14:9:18 | ... > ... | Comparison between $@ of type char and $@ of wider type int. | test.c:8:7:8:7 | c | c | test.c:7:17:7:17 | x | x |
43
| test.c:14:14:14:18 | ... < ... | Comparison between $@ of type short and $@ of wider type int. | test.c:13:8:13:8 | s | s | test.c:12:17:12:17 | x | x |

0 commit comments

Comments
 (0)