Skip to content

Commit

Permalink
[analyzer] Resolve the crash in ReturnUndefChecker
Browse files Browse the repository at this point in the history
By making sure the returned value from getKnownSVal is consistent with
the value used inside expression engine.

PR38427

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@340965 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
George Karpenkov committed Aug 29, 2018
1 parent b1014a1 commit ee49cd2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static bool isLeftShiftResultUnrepresentable(const BinaryOperator *B,
ProgramStateRef State = C.getState();
const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS()));
const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS()));
assert(LHS && RHS && "Values unknown, inconsistent state");
return (unsigned)RHS->getZExtValue() > LHS->countLeadingZeros();
}

Expand Down
1 change: 1 addition & 0 deletions lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,

const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state,
SVal V) {
V = simplifySVal(state, V);
if (V.isUnknownOrUndef())
return nullptr;

Expand Down
25 changes: 23 additions & 2 deletions test/Analysis/casts.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -analyzer-store=region -analyzer-config eagerly-assume=false -verify %s
// RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -analyzer-store=region -analyzer-config eagerly-assume=false -verify %s
// RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -analyzer-store=region -verify -analyzer-config eagerly-assume=false %s
// RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -analyzer-checker=core,alpha.core,debug.ExprInspection -analyzer-store=region -verify -analyzer-config eagerly-assume=false %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -DEAGERLY_ASSUME=1 -w %s

extern void clang_analyzer_eval(_Bool);

Expand All @@ -16,6 +17,8 @@ struct sockaddr_storage {};

void getsockname();

#ifndef EAGERLY_ASSUME

void f(int sock) {
struct sockaddr_storage storage;
struct sockaddr* sockaddr = (struct sockaddr*)&storage; // expected-warning{{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}}
Expand Down Expand Up @@ -188,3 +191,21 @@ void testSwitchWithSizeofs() {
case sizeof(char):; // no-crash
}
}

#endif

#ifdef EAGERLY_ASSUME

// expected-no-diagnostics

int globalA; // TODO: the example is not representative.
extern int globalFunc();
void no_crash_on_symsym_cast_to_long() {
char c = globalFunc() - 5;
c == 0;
globalA -= c;
globalA == 3;
(long)globalA << 48; // no-crash
}

#endif

0 comments on commit ee49cd2

Please sign in to comment.