Skip to content

Commit

Permalink
[analyzer] LoopWidening: fix crash by avoiding aliased references inv…
Browse files Browse the repository at this point in the history
…alidation

Summary: LoopWidening is invalidating references coming from type
aliases which lead to a crash.

Patch by Abbas Sabra!

Differential Revision: https://reviews.llvm.org/D80669
  • Loading branch information
AbbasNS authored and SavchenkoValeriy committed Jun 9, 2020
1 parent 7117066 commit 29353e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/StaticAnalyzer/Core/LoopWidening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState,
}

// References should not be invalidated.
auto Matches = match(findAll(stmt(hasDescendant(varDecl(hasType(referenceType())).bind(MatchRef)))),
*LCtx->getDecl()->getBody(), ASTCtx);
auto Matches = match(
findAll(stmt(hasDescendant(
varDecl(hasType(hasCanonicalType(referenceType()))).bind(MatchRef)))),
*LCtx->getDecl()->getBody(), ASTCtx);
for (BoundNodes Match : Matches) {
const VarDecl *VD = Match.getNodeAs<VarDecl>(MatchRef);
assert(VD);
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Analysis/loop-widening-preserve-reference-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ void invalid_type_region_access() {
for (int i = 0; i < 10; ++i) { }
clang_analyzer_eval(&x != 0); // expected-warning{{TRUE}}
} // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}

using AR = const A &;
void invalid_type_alias_region_access() {
AR x = B();
for (int i = 0; i < 10; ++i) {
}
clang_analyzer_eval(&x != 0); // expected-warning{{TRUE}}
} // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}

0 comments on commit 29353e6

Please sign in to comment.