C/C++: Detect ambiguous assignment of comparison results - #22336
Open
theinfosecguy wants to merge 1 commit into
Open
C/C++: Detect ambiguous assignment of comparison results#22336theinfosecguy wants to merge 1 commit into
theinfosecguy wants to merge 1 commit into
Conversation
ryao
reviewed
Aug 15, 2026
ryao
left a comment
There was a problem hiding this comment.
While I am happy you did the work for me to get this into a PR and made what appear to be improvements, would you add an Original-patch-by: to the commit message to credit my prior work?
Also, have you run your variant against any major corpora (e.g. Linux, curl, OpenZFS) to verify the lack of FPs in production code, like I did with the original version? If it helps:
| if ((status = read_status() < 0)) // BAD: assigns the comparison result. | ||
| return status; | ||
|
|
||
| if ((status = read_status()) < 0) // GOOD: assigns first, then compares. |
There was a problem hiding this comment.
This isn't the only good variant. The following is good too, because it shows that the developer really meant it.
if ((status = (read_status() < 0))) // GOOD: explicitly assigns the comparison result.
return status;
| not isExplicitlyGrouped(comparison) and | ||
| occursInCondition(assignment) and | ||
| // Assigning a comparison result to a Boolean is normally intentional. | ||
| not assignment.getLValue().getUnspecifiedType() instanceof BoolType and |
There was a problem hiding this comment.
While this is normally intentional, I believe not isExplicitlyGrouped(comparison) precludes this.
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.
Adds
cpp/ambiguous-assignment-of-comparisonto flag conditions such as:The query distinguishes this from explicitly grouped assign-then-compare and compare-then-assign expressions. It includes C and C++ tests, query help, and query-suite integration.
Local targeted and neighboring tests pass. The motivating regression is detected, and a run against
git/gitproduced no alerts.Fixes #22286