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

89 lines
2.5 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 UndefinedAssignmentChecker, a builtin check in ExprEngine that
2009-11-04 10:04:32 -05:00
// checks for assigning undefined values.
//
//===----------------------------------------------------------------------===//
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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 Checker<check::Bind> {
mutable OwningPtr<BugType> BT;
2009-12-01 06:08:04 -05:00
public:
void checkBind(SVal location, SVal val, const Stmt *S,
CheckerContext &C) const;
2009-12-01 06:08:04 -05:00
};
}
void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
const Stmt *StoreE,
CheckerContext &C) const {
2009-11-04 10:04:32 -05:00
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)
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
while (StoreE) {
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
2010-04-02 04:55:10 -04:00
if (B->isCompoundAssignmentOp()) {
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;
}
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
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
}
BugReport *R = new BugReport(*BT, str, N);
2010-04-02 04:55:10 -04:00
if (ex) {
R->addRange(ex->getSourceRange());
bugreporter::trackNullOrUndefValue(N, ex, *R);
2010-04-02 04:55:10 -04:00
}
C.emitReport(R);
2010-04-02 04:55:10 -04:00
}
2009-11-04 10:04:32 -05:00
void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
mgr.registerChecker<UndefinedAssignmentChecker>();
}