Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ loop condition on line 9 is always true because the range of

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

</overview>
<recommendation>
Expand Down
21 changes: 18 additions & 3 deletions cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ private import semmle.code.cpp.rangeanalysis.PointlessComparison
private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
import UnsignedGEZero

/** Explains a value-changing integral conversion of a constant operand. */
bindingset[side]
private string constantConversionReason(Expr operand, string side) {
if
operand.getUnconverted().getValue() !=
operand.getFullyConverted().(IntegralConversion).getValue()
Comment on lines +25 to +26
then
result =
" and the " + side + " operand evaluates to " + operand.getFullyConverted().getValue() +
" after conversion"
else result = ""
}

// Trivial comparisons of the form 1 > 0 are usually due to macro expansion.
// For example:
//
Expand Down Expand Up @@ -52,13 +65,15 @@ where
) and
leftReason = cmp.getLeftOperand().toString() + cmpOp + left.toString() and
rightReason = right.toString() + cmpOp + cmp.getRightOperand().toString() and
// If either of the operands is constant, then don't include it.
// For a constant operand, explain its value only when conversion changes it.
(
if cmp.getLeftOperand().isConstant()
then not cmp.getRightOperand().isConstant() and reason = rightReason
then
not cmp.getRightOperand().isConstant() and
reason = rightReason + constantConversionReason(cmp.getLeftOperand(), "left")
else
if cmp.getRightOperand().isConstant()
then reason = leftReason
then reason = leftReason + constantConversionReason(cmp.getRightOperand(), "right")
else reason = leftReason + " and " + rightReason
)
) and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: fix
---
* The `cpp/constant-comparison` query now explains the value used in a comparison when an integral conversion changes a constant operand.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
typedef long long frame_size_t;
#define MAX_FRAME_SIZE 18446744073709551615ULL

int invalid_frame_size(frame_size_t size) {
return size < 1024 || size >= (frame_size_t)MAX_FRAME_SIZE; // $ Alert[cpp/constant-comparison]
}

int valid_frame_size(frame_size_t size) {
return size >= 1024 && (frame_size_t)MAX_FRAME_SIZE > size; // $ Alert[cpp/constant-comparison]
}

int value_preserving_conversion(frame_size_t size) {
return size >= 1024 && size >= 1U; // $ Alert[cpp/constant-comparison]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
| ConstVirtual.cpp:24:7:24:11 | ... > ... | Comparison is always false because i <= -1. |
| ConvertedConstants.c:5:25:5:60 | ... >= ... | Comparison is always true because size >= 1024 and the right operand evaluates to -1 after conversion. |
| ConvertedConstants.c:9:26:9:60 | ... > ... | Comparison is always false because 1024 <= size and the left operand evaluates to -1 after conversion. |
| ConvertedConstants.c:13:26:13:35 | ... >= ... | Comparison is always true because size >= 1024. |
| PointlessComparison.c:7:9:7:14 | ... < ... | Comparison is always true because i <= 15. |
| PointlessComparison.c:8:9:8:15 | ... <= ... | Comparison is always true because i <= 15. |
| PointlessComparison.c:9:9:9:14 | ... > ... | Comparison is always false because i <= 15. |
Expand Down