Skip to content

Commit 67ebfb0

Browse files
committed
[clang][analyzer] Fix crash caused by overload operator member function with explicit this
1 parent cef6dbb commit 67ebfb0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ void ContainerModeling::checkPostCall(const CallEvent &Call,
157157
if (Func->isOverloadedOperator()) {
158158
const auto Op = Func->getOverloadedOperator();
159159
if (Op == OO_Equal) {
160-
// Overloaded 'operator=' must be a non-static member function.
161-
const auto *InstCall = cast<CXXInstanceCall>(&Call);
160+
// Only handle the assignment operator with implicit this
161+
const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call);
162+
if (!InstCall)
163+
return;
164+
162165
if (cast<CXXMethodDecl>(Func)->isMoveAssignmentOperator()) {
163166
handleAssignment(C, InstCall->getCXXThisVal(), Call.getOriginExpr(),
164167
Call.getArgSVal(0));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_analyze_cc1 -std=c++23 %s -verify -analyzer-checker=alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true
2+
3+
// expected-no-diagnostics
4+
5+
class ExplicitThis {
6+
int f = 0;
7+
public:
8+
ExplicitThis();
9+
ExplicitThis(ExplicitThis& other);
10+
11+
ExplicitThis& operator=(this ExplicitThis& self, ExplicitThis const& other) { // no crash
12+
self.f = other.f;
13+
return self;
14+
}
15+
16+
~ExplicitThis();
17+
};
18+
19+
void func(ExplicitThis& obj1) {
20+
obj1 = obj1;
21+
}

0 commit comments

Comments
 (0)