Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6bbe2c4

Browse files
committed
C++: Add testcase demonstrating false positive
1 parent 9a84163 commit 6bbe2c4

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
1414
| test.cpp:84:7:84:11 | ... = ... | Use of '=' where '==' may have been intended. |
1515
| test.cpp:92:17:92:22 | ... = ... | Use of '=' where '==' may have been intended. |
1616
| test.cpp:113:6:113:10 | ... = ... | Use of '=' where '==' may have been intended. |
17+
| test.cpp:129:17:129:21 | ... = ... | Use of '=' where '==' may have been intended. |
18+
| test.cpp:134:19:134:23 | ... = ... | Use of '=' where '==' may have been intended. |
19+
| test.cpp:138:21:138:25 | ... = ... | Use of '=' where '==' may have been intended. |
20+
| test.cpp:141:7:141:11 | ... = ... | Use of '=' where '==' may have been intended. |
21+
| test.cpp:144:32:144:36 | ... = ... | Use of '=' where '==' may have been intended. |
22+
| test.cpp:147:41:147:45 | ... = ... | Use of '=' where '==' may have been intended. |
23+
| test.cpp:150:32:150:36 | ... = ... | Use of '=' where '==' may have been intended. |
24+
| test.cpp:153:46:153:50 | ... = ... | Use of '=' where '==' may have been intended. |

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,33 @@ void f2() {
123123

124124
if(sz = "def") { // GOOD: a == comparison with a string literal is probably not the intent here
125125
}
126+
}
127+
128+
void f3(int x, int y) {
129+
if(x == 1 && (y = 2)) { // GOOD [FALSE POSITIVE]: the programmer seems to be okay with unparenthesized
130+
// comparison operands, so the parenthesis was used to mark this
131+
// as an assignment
132+
}
133+
134+
if((x == 1) && (y = 2)) { // BAD
135+
}
136+
137+
long z = x;
138+
if(((z == 42) || (y = 2)) && (x == 1)) { // BAD
139+
}
140+
141+
if((y = 2) && (x == z || x == 1)) { // GOOD [FALSE POSITIVE]
142+
}
143+
144+
if(((x == 42) || x == 1) && (y = 2)) { // BAD
145+
}
146+
147+
if(x == 10 || (x == 42 && x == 1) && (y = 2)) { // GOOD [FALSE POSITIVE]
148+
}
149+
150+
if(x == 10 || ((x == 42) && (y = 2)) && (z == 1)) { // BAD
151+
}
152+
153+
if((x == 10) || ((z == z) && (x == 1)) && (y = 2)) { // BAD
154+
}
126155
}

0 commit comments

Comments
 (0)