2009-11-04 12:24:16 +08:00
|
|
|
//===--- UndefinedAssignmentChecker.h ---------------------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-02-28 09:27:37 +08:00
|
|
|
// This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that
|
2009-11-04 12:24:16 +08:00
|
|
|
// checks for assigning undefined values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerV2.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefinedAssignmentChecker
|
2011-02-28 09:27:37 +08:00
|
|
|
: public CheckerV2<check::Bind> {
|
|
|
|
mutable llvm::OwningPtr<BugType> BT;
|
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
public:
|
2011-02-28 09:27:37 +08:00
|
|
|
void checkBind(SVal location, SVal val, CheckerContext &C) const;
|
2009-11-22 20:29:52 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
|
|
|
|
CheckerContext &C) const {
|
2009-11-04 12:24:16 +08:00
|
|
|
if (!val.isUndef())
|
|
|
|
return;
|
|
|
|
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink();
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2010-03-23 06:16:26 +08:00
|
|
|
const char *str = "Assigned value is garbage or undefined";
|
|
|
|
|
2009-11-04 12:24:16 +08:00
|
|
|
if (!BT)
|
2011-02-28 09:27:37 +08:00
|
|
|
BT.reset(new BuiltinBug(str));
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2010-03-23 06:16:26 +08:00
|
|
|
const Expr *ex = 0;
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
const Stmt *StoreE = C.getStmt();
|
2010-09-02 08:56:20 +08:00
|
|
|
while (StoreE) {
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
|
2010-03-23 06:16:26 +08:00
|
|
|
if (B->isCompoundAssignmentOp()) {
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
if (state->getSVal(B->getLHS()).isUndef()) {
|
|
|
|
str = "The left expression of the compound assignment is an "
|
|
|
|
"uninitialized value. The computed value will also be garbage";
|
|
|
|
ex = B->getLHS();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 08:42:23 +08:00
|
|
|
|
|
|
|
ex = B->getRHS();
|
2010-03-23 06:16:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:56:20 +08:00
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
|
2009-11-05 08:42:23 +08:00
|
|
|
const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
ex = VD->getInit();
|
|
|
|
}
|
2010-03-23 06:16:26 +08:00
|
|
|
|
|
|
|
break;
|
2009-11-04 12:24:16 +08:00
|
|
|
}
|
|
|
|
|
2010-03-23 06:16:26 +08:00
|
|
|
EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N);
|
|
|
|
if (ex) {
|
|
|
|
R->addRange(ex->getSourceRange());
|
|
|
|
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
|
|
|
|
}
|
2009-11-04 12:24:16 +08:00
|
|
|
C.EmitReport(R);
|
2010-03-23 06:16:26 +08:00
|
|
|
}
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedAssignmentChecker>();
|
|
|
|
}
|