Skip to content

Commit

Permalink
[analyzer] pr37209: Fix casts of glvalues to references.
Browse files Browse the repository at this point in the history
Many glvalue expressions aren't of their respective reference type -
they are simply glvalues of their value type.

This was causing problems when we were trying to obtain type of the original
expression while evaluating certain glvalue bit-casts.

Fixed by artificially forging a reference type to provide to the casting
procedure.

Differential Revision: https://reviews.llvm.org/D46224

llvm-svn: 331558
  • Loading branch information
haoNoQ committed May 4, 2018
1 parent 5b39acd commit 2fd6aa7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
Expand Up @@ -257,6 +257,13 @@ ProgramStateRef ExprEngine::handleLValueBitCast(
ProgramStateRef state, const Expr* Ex, const LocationContext* LCtx,
QualType T, QualType ExTy, const CastExpr* CastE, StmtNodeBuilder& Bldr,
ExplodedNode* Pred) {
if (T->isLValueReferenceType()) {
assert(!CastE->getType()->isLValueReferenceType());
ExTy = getContext().getLValueReferenceType(ExTy);
} else if (T->isRValueReferenceType()) {
assert(!CastE->getType()->isRValueReferenceType());
ExTy = getContext().getRValueReferenceType(ExTy);
}
// Delegate to SValBuilder to process.
SVal OrigV = state->getSVal(Ex, LCtx);
SVal V = svalBuilder.evalCast(OrigV, T, ExTy);
Expand Down
14 changes: 14 additions & 0 deletions clang/test/Analysis/casts.cpp
Expand Up @@ -21,3 +21,17 @@ void intAsBoolAsSwitchCondition(int c) {
break;
}
}

int *&castToIntPtrLValueRef(char *p) {
return (int *&)*(int *)p;
}
bool testCastToIntPtrLValueRef(char *p, int *s) {
return castToIntPtrLValueRef(p) != s; // no-crash
}

int *&&castToIntPtrRValueRef(char *p) {
return (int *&&)*(int *)p;
}
bool testCastToIntPtrRValueRef(char *p, int *s) {
return castToIntPtrRValueRef(p) != s; // no-crash
}

0 comments on commit 2fd6aa7

Please sign in to comment.