opnsense-src/lib/Checker/UndefinedAssignmentChecker.cpp

80 lines
2.3 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 GRExprEngine that
// checks for assigning undefined values.
//
//===----------------------------------------------------------------------===//
2009-12-01 06:08:04 -05:00
#include "GRExprEngineInternalChecks.h"
2010-02-16 04:31:36 -05:00
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
#include "clang/Checker/BugReporter/BugReporter.h"
2009-11-04 10:04:32 -05:00
using namespace clang;
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 *AssignE,
const Stmt *StoreE, SVal location,
SVal val);
};
}
void clang::RegisterUndefinedAssignmentChecker(GRExprEngine &Eng){
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 *AssignE,
const Stmt *StoreE,
2009-11-04 10:04:32 -05:00
SVal location,
SVal val) {
if (!val.isUndef())
return;
2009-12-01 06:08:04 -05:00
ExplodedNode *N = C.GenerateSink();
2009-11-04 10:04:32 -05:00
if (!N)
return;
if (!BT)
2009-11-18 09:59:57 -05:00
BT = new BuiltinBug("Assigned value is garbage or undefined");
2009-11-04 10:04:32 -05:00
// Generate a report for this bug.
2009-11-18 09:59:57 -05:00
EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
2009-11-04 10:04:32 -05:00
2009-11-05 12:18:09 -05:00
if (AssignE) {
const Expr *ex = 0;
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE))
ex = B->getRHS();
else if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) {
const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
ex = VD->getInit();
}
if (ex) {
R->addRange(ex->getSourceRange());
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
}
2009-11-04 10:04:32 -05:00
}
C.EmitReport(R);
}