Skip to content

[analyzer] Support PointerType in getCXXRecordDecl for ContainerModeling #87787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}
}
}