diff --git a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp index 2876b31220a7..74dda3e73693 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp +++ b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp @@ -14,9 +14,10 @@ loop condition on line 9 is always true because the range of

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 x >= y might read: "Comparison is always false -because x >= 5 and 3 >= y."

+understand why a result was reported. For example, the comparison +x <= y 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.

diff --git a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql index 916f664c4fad..5ca730ba069f 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql +++ b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql @@ -18,6 +18,26 @@ private import semmle.code.cpp.rangeanalysis.PointlessComparison private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils import UnsignedGEZero +/** Gets `expr` without any enclosing parentheses. */ +private Expr stripParentheses(Expr expr) { + if expr instanceof ParenthesisExpr + then result = stripParentheses(expr.(ParenthesisExpr).getExpr()) + else result = expr +} + +/** Explains a value-changing integral conversion of a constant operand. */ +bindingset[side] +private string constantConversionReason(Expr operand, string side) { + if + operand.getUnconverted().getValue() != + stripParentheses(operand.getFullyConverted()).(IntegralConversion).getValue() + 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: // @@ -52,13 +72,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 diff --git a/cpp/ql/src/change-notes/2026-11-23-constant-comparison-conversions.md b/cpp/ql/src/change-notes/2026-11-23-constant-comparison-conversions.md new file mode 100644 index 000000000000..b23d060319b3 --- /dev/null +++ b/cpp/ql/src/change-notes/2026-11-23-constant-comparison-conversions.md @@ -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. diff --git a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c new file mode 100644 index 000000000000..106cccdbf5b7 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c @@ -0,0 +1,26 @@ +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] +} + +int parenthesized_right_conversion(frame_size_t size) { + return size < 1024 || size >= ((frame_size_t)MAX_FRAME_SIZE); // $ Alert[cpp/constant-comparison] +} + +int parenthesized_left_conversion(frame_size_t size) { + return size >= 1024 && (((frame_size_t)MAX_FRAME_SIZE)) > size; // $ Alert[cpp/constant-comparison] +} + +int parenthesized_value_preserving_conversion(frame_size_t size) { + return size >= 1024 && size >= ((frame_size_t)1U); // $ Alert[cpp/constant-comparison] +} diff --git a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected index d00c38fda284..87091280a434 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected @@ -1,4 +1,10 @@ | 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. | +| ConvertedConstants.c:17:25:17:62 | ... >= ... | Comparison is always true because size >= 1024 and the right operand evaluates to -1 after conversion. | +| ConvertedConstants.c:21:26:21:64 | ... > ... | Comparison is always false because 1024 <= size and the left operand evaluates to -1 after conversion. | +| ConvertedConstants.c:25:26:25:51 | ... >= ... | 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. | @@ -49,5 +55,5 @@ | PointlessComparison.cpp:42:6:42:29 | ... >= ... | Comparison is always false because ... >> ... <= 140737488355327. | | PointlessComparison.cpp:43:6:43:29 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327. | | PointlessComparison.cpp:44:6:44:28 | ... >= ... | Comparison is always true because ... >> ... >= 140737488355327. | -| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. | +| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295 and the right operand evaluates to 4294967295 after conversion. | | Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |