Skip to content

Commit 8bb5bdb

Browse files
committed
C++: Explain converted constants in comparison alerts
1 parent aafa7de commit 8bb5bdb

5 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ loop condition on line 9 is always true because the range of
1414

1515
<p>The bounds which were deduced for the left and right operands of the
1616
comparison are included in the message as they often make it easier to
17-
understand why a result was reported. For example the message for the
18-
comparison <code>x >= y</code> might read: "Comparison is always false
19-
because x >= 5 and 3 >= y."</p>
17+
understand why a result was reported. For example, the comparison
18+
<code>x &lt;= y</code> might be reported as "Comparison is always false
19+
because x >= 5 and 3 >= y." If an integer conversion changes a constant
20+
operand, the message also reports the value used in the comparison.</p>
2021

2122
</overview>
2223
<recommendation>

‎cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ private import semmle.code.cpp.rangeanalysis.PointlessComparison
1818
private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1919
import UnsignedGEZero
2020

21+
/** Explains a value-changing integral conversion of a constant operand. */
22+
bindingset[side]
23+
private string constantConversionReason(Expr operand, string side) {
24+
if
25+
operand.getUnconverted().getValue() !=
26+
operand.getFullyConverted().(IntegralConversion).getValue()
27+
then
28+
result =
29+
" and the " + side + " operand evaluates to " + operand.getFullyConverted().getValue() +
30+
" after conversion"
31+
else result = ""
32+
}
33+
2134
// Trivial comparisons of the form 1 > 0 are usually due to macro expansion.
2235
// For example:
2336
//
@@ -52,13 +65,15 @@ where
5265
) and
5366
leftReason = cmp.getLeftOperand().toString() + cmpOp + left.toString() and
5467
rightReason = right.toString() + cmpOp + cmp.getRightOperand().toString() and
55-
// If either of the operands is constant, then don't include it.
68+
// For a constant operand, explain its value only when conversion changes it.
5669
(
5770
if cmp.getLeftOperand().isConstant()
58-
then not cmp.getRightOperand().isConstant() and reason = rightReason
71+
then
72+
not cmp.getRightOperand().isConstant() and
73+
reason = rightReason + constantConversionReason(cmp.getLeftOperand(), "left")
5974
else
6075
if cmp.getRightOperand().isConstant()
61-
then reason = leftReason
76+
then reason = leftReason + constantConversionReason(cmp.getRightOperand(), "right")
6277
else reason = leftReason + " and " + rightReason
6378
)
6479
) and
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* The `cpp/constant-comparison` query now explains the value used in a comparison when an integral conversion changes a constant operand.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
typedef long long frame_size_t;
2+
#define MAX_FRAME_SIZE 18446744073709551615ULL
3+
4+
int invalid_frame_size(frame_size_t size) {
5+
return size < 1024 || size >= (frame_size_t)MAX_FRAME_SIZE; // $ Alert[cpp/constant-comparison]
6+
}
7+
8+
int valid_frame_size(frame_size_t size) {
9+
return size >= 1024 && (frame_size_t)MAX_FRAME_SIZE > size; // $ Alert[cpp/constant-comparison]
10+
}
11+
12+
int value_preserving_conversion(frame_size_t size) {
13+
return size >= 1024 && size >= 1U; // $ Alert[cpp/constant-comparison]
14+
}

‎cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
| ConstVirtual.cpp:24:7:24:11 | ... > ... | Comparison is always false because i <= -1. |
2+
| ConvertedConstants.c:5:25:5:60 | ... >= ... | Comparison is always true because size >= 1024 and the right operand evaluates to -1 after conversion. |
3+
| ConvertedConstants.c:9:26:9:60 | ... > ... | Comparison is always false because 1024 <= size and the left operand evaluates to -1 after conversion. |
4+
| ConvertedConstants.c:13:26:13:35 | ... >= ... | Comparison is always true because size >= 1024. |
25
| PointlessComparison.c:7:9:7:14 | ... < ... | Comparison is always true because i <= 15. |
36
| PointlessComparison.c:8:9:8:15 | ... <= ... | Comparison is always true because i <= 15. |
47
| PointlessComparison.c:9:9:9:14 | ... > ... | Comparison is always false because i <= 15. |

0 commit comments

Comments
 (0)