2009-11-04 10:04:32 -05: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-05-02 15:39:53 -04:00
|
|
|
// This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that
|
2009-11-04 10:04:32 -05:00
|
|
|
// checks for assigning undefined values.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2011-05-02 15:39:53 -04:00
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-20 08:06:31 -05:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2009-11-04 10:04:32 -05:00
|
|
|
|
|
|
|
|
using namespace clang;
|
2011-02-20 08:06:31 -05:00
|
|
|
using namespace ento;
|
2009-11-04 10:04:32 -05:00
|
|
|
|
2009-12-01 06:08:04 -05:00
|
|
|
namespace {
|
|
|
|
|
class UndefinedAssignmentChecker
|
2011-05-02 15:39:53 -04:00
|
|
|
: public Checker<check::Bind> {
|
2012-04-14 10:01:31 -04:00
|
|
|
mutable OwningPtr<BugType> BT;
|
2011-05-02 15:39:53 -04:00
|
|
|
|
2009-12-01 06:08:04 -05:00
|
|
|
public:
|
2011-10-20 17:14:49 -04:00
|
|
|
void checkBind(SVal location, SVal val, const Stmt *S,
|
|
|
|
|
CheckerContext &C) const;
|
2009-12-01 06:08:04 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-02 15:39:53 -04:00
|
|
|
void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
|
2011-10-20 17:14:49 -04:00
|
|
|
const Stmt *StoreE,
|
2011-05-02 15:39:53 -04:00
|
|
|
CheckerContext &C) const {
|
2009-11-04 10:04:32 -05:00
|
|
|
if (!val.isUndef())
|
|
|
|
|
return;
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
ExplodedNode *N = C.generateSink();
|
2009-11-04 10:04:32 -05:00
|
|
|
|
|
|
|
|
if (!N)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-04-02 04:55:10 -04:00
|
|
|
const char *str = "Assigned value is garbage or undefined";
|
|
|
|
|
|
2009-11-04 10:04:32 -05:00
|
|
|
if (!BT)
|
2011-05-02 15:39:53 -04:00
|
|
|
BT.reset(new BuiltinBug(str));
|
2009-11-04 10:04:32 -05:00
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2010-04-02 04:55:10 -04:00
|
|
|
const Expr *ex = 0;
|
2009-11-04 10:04:32 -05:00
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
while (StoreE) {
|
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
|
2010-04-02 04:55:10 -04:00
|
|
|
if (B->isCompoundAssignmentOp()) {
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef state = C.getState();
|
|
|
|
|
if (state->getSVal(B->getLHS(), C.getLocationContext()).isUndef()) {
|
2010-04-02 04:55:10 -04:00
|
|
|
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 12:18:09 -05:00
|
|
|
|
|
|
|
|
ex = B->getRHS();
|
2010-04-02 04:55:10 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
|
2011-10-20 17:14:49 -04:00
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
2009-11-05 12:18:09 -05:00
|
|
|
ex = VD->getInit();
|
|
|
|
|
}
|
2010-04-02 04:55:10 -04:00
|
|
|
|
|
|
|
|
break;
|
2009-11-04 10:04:32 -05:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
BugReport *R = new BugReport(*BT, str, N);
|
2010-04-02 04:55:10 -04:00
|
|
|
if (ex) {
|
|
|
|
|
R->addRange(ex->getSourceRange());
|
2012-12-02 08:20:44 -05:00
|
|
|
bugreporter::trackNullOrUndefValue(N, ex, *R);
|
2010-04-02 04:55:10 -04:00
|
|
|
}
|
2012-12-02 08:20:44 -05:00
|
|
|
C.emitReport(R);
|
2010-04-02 04:55:10 -04:00
|
|
|
}
|
2009-11-04 10:04:32 -05:00
|
|
|
|
2011-05-02 15:39:53 -04:00
|
|
|
void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
|
|
|
|
|
mgr.registerChecker<UndefinedAssignmentChecker>();
|
|
|
|
|
}
|