Repository navigation
C++: Explain converted constants in comparison alerts - #23202
Open
SkyCipherr wants to merge 2 commits into
Open
SkyCipherr wants to merge 2 commits into
SkyCipherr wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Parenthesized casts bypass integral-conversion detection, causing alerts to lose the -1 explanation; the query needs to handle trailing parentheses and add a regression case.
1 open issue
What changed in this PR
This PR improves cpp/constant-comparison alerts by explaining converted constant values alongside inferred operand bounds, addressing the unclear diagnostics in #11744.
Changes:
- Adds converted-value explanations for constant operands on either side of a comparison.
- Adds regression cases for value-changing and value-preserving conversions.
- Updates query help and adds a release note.
| File | Description |
|---|---|
cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected |
Records expected alerts for the new regression cases. |
cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/ConvertedConstants.c |
Tests conversions on both sides and an unchanged constant value. |
cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql |
Adds converted constant values to alert explanations. |
cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.qhelp |
Documents the explanation and corrects the comparison example. |
cpp/ql/src/change-notes/2026-11-23-constant-comparison-conversions.md |
Announces the diagnostic improvement. |
Review effort: Balanced (adaptive)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

When a constant operand changes value during integral conversion, include the converted value in the
cpp/constant-comparisonalert. In the reduced libarchive example, the message explains that the constant on the right evaluates to-1, alongside the inferred bound on the left.Regression cases cover constants on either side, single and nested trailing parentheses, and value-preserving conversions. Both tests in the
PointlessComparisonsuite pass with CodeQL CLI 2.27.4. The follow-up retains all 56 earlier alert locations and adds three fixture alerts. It also adds the converted-value explanation to the existing parenthesized((size_t)-1)case; no earlier alert is removed. The C fixture passes GCC C11 syntax checking, QL formatting passes, andgit diff --checkpasses.These are local results; upstream CI is separate.
Fixes #11744.