From 8bb5bdbb713d410f10e2ab40bbe1df86aa45225c Mon Sep 17 00:00:00 2001 From: SkyCipherr Date: Mon, 23 Nov 2026 14:10:13 +0800 Subject: [PATCH 1/2] C++: Explain converted constants in comparison alerts --- .../Arithmetic/PointlessComparison.qhelp | 7 ++++--- .../Arithmetic/PointlessComparison.ql | 21 ++++++++++++++++--- ...6-11-23-constant-comparison-conversions.md | 4 ++++ .../PointlessComparison/ConvertedConstants.c | 14 +++++++++++++ .../PointlessComparison.expected | 3 +++ 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 cpp/ql/src/change-notes/2026-11-23-constant-comparison-conversions.md create mode 100644 cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c 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..a52c7180b23c 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql +++ b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql @@ -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() + 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 +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 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..30818f8584b6 --- /dev/null +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c @@ -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] +} 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..598a9b61462d 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,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. | From c41e17342f515610addf0169bb20c0055a752cdf Mon Sep 17 00:00:00 2001 From: SkyCipherr Date: Mon, 30 Nov 2026 11:01:23 +0800 Subject: [PATCH 2/2] C++: Explain parenthesized constant conversions --- .../Likely Bugs/Arithmetic/PointlessComparison.ql | 9 ++++++++- .../PointlessComparison/ConvertedConstants.c | 12 ++++++++++++ .../PointlessComparison/PointlessComparison.expected | 5 ++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql index a52c7180b23c..5ca730ba069f 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql +++ b/cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql @@ -18,12 +18,19 @@ 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() != - operand.getFullyConverted().(IntegralConversion).getValue() + stripParentheses(operand.getFullyConverted()).(IntegralConversion).getValue() then result = " and the " + side + " operand evaluates to " + operand.getFullyConverted().getValue() + 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 index 30818f8584b6..106cccdbf5b7 100644 --- a/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c +++ b/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c @@ -12,3 +12,15 @@ int valid_frame_size(frame_size_t size) { 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 598a9b61462d..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 @@ -2,6 +2,9 @@ | 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. | @@ -52,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. |