opnsense-src/lib/Checker/UndefinedAssignmentChecker.cpp

96 lines
2.7 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-04-02 04:55:10 -04:00
#include "clang/Checker/BugReporter/BugType.h"
2010-02-16 04:31:36 -05:00
#include "clang/Checker/PathSensitive/CheckerVisitor.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;
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
2010-04-02 04:55:10 -04:00
while (AssignE) {
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE)) {
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>(AssignE)) {
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