Skip to content

Commit d144f0d

Browse files
committed
C++: Test for unreachable return statement
This test shows that the previous fix did not solve the problem where a bad return statement exists but is unreachable.
1 parent 3c6bed4 commit d144f0d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,43 @@ class AlwaysThrows {
133133
}
134134
};
135135

136+
class Reachability {
137+
Reachability &operator=(Reachability &that) { // GOOD [FALSE POSITIVE]
138+
int one = 1;
139+
if (one)
140+
return *this;
141+
else
142+
return that; // unreachable
143+
}
144+
145+
// helper function that always returns a reference to `*this`.
146+
Reachability &returnThisReference() {
147+
int one = 1;
148+
if (one)
149+
return *this;
150+
else
151+
return staticInstance; // unreachable
152+
}
153+
154+
// helper function that always returns `this`.
155+
Reachability *const returnThisPointer() {
156+
int one = 1;
157+
if (one)
158+
return this;
159+
else
160+
return &staticInstance; // unreachable
161+
}
162+
163+
Reachability &operator=(int _val) { // GOOD [FALSE POSITIVE]
164+
return returnThisReference();
165+
}
166+
167+
Reachability &operator=(short _val) { // GOOD [FALSE POSITIVE]
168+
return *returnThisPointer();
169+
}
170+
171+
static Reachability staticInstance;
172+
};
136173

137174
int main() {
138175
Container c;

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 82/AV Rule 82.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
| AV Rule 82.cpp:24:8:24:16 | operator= | Assignment operator in class Bad2 should have return type Bad2&. Otherwise a copy is created at each call. |
33
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<T> does not return a reference to *this. |
44
| AV Rule 82.cpp:63:29:63:37 | operator= | Assignment operator in class TemplateReturnAssignment<int> does not return a reference to *this. |
5+
| AV Rule 82.cpp:137:17:137:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |
6+
| AV Rule 82.cpp:163:17:163:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |
7+
| AV Rule 82.cpp:167:17:167:25 | operator= | Assignment operator in class Reachability does not return a reference to *this. |

0 commit comments

Comments
 (0)