Skip to content

Commit

Permalink
[analyzer] Support PointerType in getCXXRecordDecl for `Container…
Browse files Browse the repository at this point in the history
…Modeling` (#87787)
  • Loading branch information
shenjunjiekoda committed Apr 12, 2024
1 parent 8d468c1 commit 05d8b5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ const CXXRecordDecl *getCXXRecordDecl(ProgramStateRef State,
Type = RefT->getPointeeType();
}

if (const auto *PtrT = Type->getAs<PointerType>()) {
Type = PtrT->getPointeeType();
}

return Type->getUnqualifiedDesugaredType()->getAsCXXRecordDecl();
}

Expand Down
10 changes: 9 additions & 1 deletion clang/test/Analysis/invalidated-iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ struct cont_with_ptr_iterator {
T* erase(T*);
};

void invalidated_access_via_end_iterator_after_push_back() {
cont_with_ptr_iterator<int> C;
C.push_back(1);
auto i = C.end();
C.push_back(2);
auto j = i[-1]; // expected-warning{{Invalidated iterator accessed}}
}

void invalidated_dereference_end_ptr_iterator(cont_with_ptr_iterator<int> &C) {
auto i = C.begin();
C.erase(i);
Expand Down Expand Up @@ -196,4 +204,4 @@ void invalidated_subscript_end_ptr_iterator(cont_with_ptr_iterator<int> &C) {
auto i = C.begin();
C.erase(i);
(void) i[1]; // expected-warning{{Invalidated iterator accessed}}
}
}

0 comments on commit 05d8b5e

Please sign in to comment.