Skip to content

Commit e60fe2e

Browse files
authored
[clang][analyzer] Fix InvalidatedIterator crash caused by overload operator member function with explicit this (#132581)
Fixes #116372 From this PR #83585, CSA starts to model overload operator member function with explicit this as `SimpleFunctionCall` rather than `CXXMemberOperatorCall` (derived from `CXXInstanceCall`), so `CXXInstanceCall` only represents a non-static C++ member function call `with implicit this`. For this checker, it models `operator=` for STL containers, which always uses implicit this, so the situation using explicit this can be skipped directly.
1 parent ef56f4b commit e60fe2e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp

+5-2
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));

clang/test/Analysis/invalidated-iterator.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=false %s -verify
22
// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 %s -verify
3+
// RUN: %clang_analyze_cc1 -std=c++23 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 %s -verify
34

45
#include "Inputs/system-header-simulator-cxx.h"
56

@@ -204,4 +205,26 @@ void invalidated_subscript_end_ptr_iterator(cont_with_ptr_iterator<int> &C) {
204205
auto i = C.begin();
205206
C.erase(i);
206207
(void) i[1]; // expected-warning{{Invalidated iterator accessed}}
207-
}
208+
}
209+
210+
#if __cplusplus >= 202302L
211+
namespace GH116372 {
212+
class ExplicitThis {
213+
int f = 0;
214+
public:
215+
ExplicitThis();
216+
ExplicitThis(ExplicitThis& other);
217+
218+
ExplicitThis& operator=(this ExplicitThis& self, ExplicitThis const& other) { // no crash
219+
self.f = other.f;
220+
return self;
221+
}
222+
223+
~ExplicitThis();
224+
};
225+
226+
void func(ExplicitThis& obj1) {
227+
obj1 = obj1;
228+
}
229+
}
230+
#endif

0 commit comments

Comments
 (0)