Skip to content

Commit ab58cb3

Browse files
authored
Merge pull request github#5604 from MathiasVP/fix-false-positive-in-assign-where-compare-meant
C++: Fix FP in cpp/assign-where-compare-meant
2 parents f0491af + 5eb1f8a commit ab58cb3

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The 'Assignment where comparison was intended' (cpp/assign-where-compare-meant) query has been improved to flag fewer benign assignments in conditionals.

cpp/ql/src/Likely Bugs/Likely Typos/AssignWhereCompareMeant.ql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,29 @@ class BooleanControllingAssignmentInExpr extends BooleanControllingAssignment {
5454
override predicate isWhitelisted() {
5555
this.getConversion().(ParenthesisExpr).isParenthesised()
5656
or
57-
// whitelist this assignment if all comparison operations in the expression that this
57+
// Allow this assignment if all comparison operations in the expression that this
5858
// assignment is part of, are not parenthesized. In that case it seems like programmer
5959
// is fine with unparenthesized comparison operands to binary logical operators, and
6060
// the parenthesis around this assignment was used to call it out as an assignment.
6161
this.isParenthesised() and
6262
forex(ComparisonOperation op | op = getComparisonOperand*(this.getParent+()) |
6363
not op.isParenthesised()
6464
)
65+
or
66+
// Match a pattern like:
67+
// ```
68+
// if((a = b) && use_value(a)) { ... }
69+
// ```
70+
// where the assignment is meant to update the value of `a` before it's used in some other boolean
71+
// subexpression that is guarenteed to be evaluate _after_ the assignment.
72+
this.isParenthesised() and
73+
exists(LogicalAndExpr parent, Variable var, VariableAccess access |
74+
var = this.getLValue().(VariableAccess).getTarget() and
75+
access = var.getAnAccess() and
76+
not access.isUsedAsLValue() and
77+
parent.getRightOperand() = access.getParent*() and
78+
parent.getLeftOperand() = this.getParent*()
79+
)
6580
}
6681
}
6782

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/AssignWhereCompareMeant/AssignWhereCompareMeant.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919
| test.cpp:144:32:144:36 | ... = ... | Use of '=' where '==' may have been intended. |
2020
| test.cpp:150:32:150:36 | ... = ... | Use of '=' where '==' may have been intended. |
2121
| test.cpp:153:46:153:50 | ... = ... | Use of '=' where '==' may have been intended. |
22+
| test.cpp:166:22:166:27 | ... = ... | Use of '=' where '==' may have been intended. |
23+
| test.cpp:168:24:168:29 | ... = ... | Use of '=' where '==' may have been intended. |
24+
| test.cpp:169:23:169:28 | ... = ... | Use of '=' where '==' may have been intended. |
25+
| test.cpp:171:7:171:12 | ... = ... | Use of '=' where '==' may have been intended. |

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/AssignWhereCompareMeant/test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,21 @@ void f3(int x, int y) {
153153
if((x == 10) || ((z == z) && (x == 1)) && (y = 2)) { // BAD
154154
}
155155
}
156+
157+
bool use(int);
158+
159+
void f4(int x, bool b) {
160+
if((x = 10) && use(x)) {} // GOOD: This is likely just a short-hand way of writing an assignment
161+
// followed by a boolean check.
162+
if((x = 10) && b && use(x)) {} // GOOD: Same reason as above
163+
if((x = 10) && use(x) && b) {} // GOOD: Same reason as above
164+
if((x = 10) && (use(x) && b)) {} // GOOD: Same reason as above
165+
166+
if(use(x) && b && (x = 10)) {} // BAD: The assignment is the last thing that happens in the comparison.
167+
// This doesn't match the usual pattern.
168+
if((use(x) && b) && (x = 10)) {} // BAD: Same reason as above
169+
if(use(x) && (b && (x = 10))) {} // BAD: Same reason as above
170+
171+
if((x = 10) || use(x)) {} // BAD: This doesn't follow the usual style of writing an assignment in
172+
// a boolean check.
173+
}

0 commit comments

Comments
 (0)