opnsense-src/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp

95 lines
2.6 KiB
C++
Raw Normal View History

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.
//
//===----------------------------------------------------------------------===//
//
// This defines UndefinedAssginmentChecker, a builtin check in ExprEngine that
2009-11-04 10:04:32 -05:00
// checks for assigning undefined values.
//
//===----------------------------------------------------------------------===//
#include "InternalChecks.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
2009-11-04 10:04:32 -05:00
using namespace clang;
using namespace ento;
2009-11-04 10:04:32 -05:00
2009-12-01 06:08:04 -05:00
namespace {
class UndefinedAssignmentChecker
: public CheckerVisitor<UndefinedAssignmentChecker> {
BugType *BT;
public:
UndefinedAssignmentChecker() : BT(0) {}
static void *getTag();
virtual void PreVisitBind(CheckerContext &C, const Stmt *StoreE,
SVal location, SVal val);
2009-12-01 06:08:04 -05:00
};
}
void ento::RegisterUndefinedAssignmentChecker(ExprEngine &Eng){
2009-12-01 06:08:04 -05:00
Eng.registerCheck(new UndefinedAssignmentChecker());
}
2009-11-04 10:04:32 -05:00
void *UndefinedAssignmentChecker::getTag() {
static int x = 0;
return &x;
}
2009-11-05 12:18:09 -05:00
void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
const Stmt *StoreE,
2009-11-04 10:04:32 -05:00
SVal location,
SVal val) {
if (!val.isUndef())
return;
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)
2010-04-02 04:55:10 -04:00
BT = 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
while (StoreE) {
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
2010-04-02 04:55:10 -04: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 12:18:09 -05:00
ex = B->getRHS();
2010-04-02 04:55:10 -04:00
break;
}
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
2009-11-05 12:18:09 -05:00
const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
ex = VD->getInit();
}
2010-04-02 04:55:10 -04:00
break;
2009-11-04 10:04:32 -05:00
}
2010-04-02 04:55:10 -04:00
EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N);
if (ex) {
R->addRange(ex->getSourceRange());
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
}
2009-11-04 10:04:32 -05:00
C.EmitReport(R);
2010-04-02 04:55:10 -04:00
}
2009-11-04 10:04:32 -05:00