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

54 lines
1.8 KiB
C++
Raw Normal View History

2009-11-18 09:59:57 -05:00
//===--- UndefinedArraySubscriptChecker.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 UndefinedArraySubscriptChecker, a builtin check in ExprEngine
2009-11-18 09:59:57 -05:00
// that performs checks for undefined array subscripts.
//
//===----------------------------------------------------------------------===//
#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-18 09:59:57 -05:00
using namespace clang;
using namespace ento;
2009-11-18 09:59:57 -05:00
namespace {
2009-12-01 06:08:04 -05:00
class UndefinedArraySubscriptChecker
: public Checker< check::PreStmt<ArraySubscriptExpr> > {
mutable OwningPtr<BugType> BT;
2009-11-18 09:59:57 -05:00
public:
void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
2009-11-18 09:59:57 -05:00
};
} // end anonymous namespace
void
UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
CheckerContext &C) const {
if (C.getState()->getSVal(A->getIdx(), C.getLocationContext()).isUndef()) {
if (ExplodedNode *N = C.generateSink()) {
2009-11-18 09:59:57 -05:00
if (!BT)
BT.reset(new BuiltinBug("Array subscript is undefined"));
2009-11-18 09:59:57 -05:00
// Generate a report for this bug.
BugReport *R = new BugReport(*BT, BT->getName(), N);
2009-11-18 09:59:57 -05:00
R->addRange(A->getIdx()->getSourceRange());
bugreporter::trackNullOrUndefValue(N, A->getIdx(), *R);
C.emitReport(R);
2009-11-18 09:59:57 -05:00
}
}
}
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
mgr.registerChecker<UndefinedArraySubscriptChecker>();
}