2009-11-11 21:42:54 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-23 02:53:44 +08:00
|
|
|
// This defines UndefinedArraySubscriptChecker, a builtin check in ExprEngine
|
2009-11-11 21:42:54 +08:00
|
|
|
// that performs checks for undefined array subscripts.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:41 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2009-11-11 21:42:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-11 21:42:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefinedArraySubscriptChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PreStmt<ArraySubscriptExpr> > {
|
2011-02-28 09:27:41 +08:00
|
|
|
mutable llvm::OwningPtr<BugType> BT;
|
|
|
|
|
2009-11-11 21:42:54 +08:00
|
|
|
public:
|
2011-02-28 09:27:41 +08:00
|
|
|
void checkPreStmt(const ArraySubscriptExpr *A, CheckerContext &C) const;
|
2009-11-11 21:42:54 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void
|
2011-02-28 09:27:41 +08:00
|
|
|
UndefinedArraySubscriptChecker::checkPreStmt(const ArraySubscriptExpr *A,
|
|
|
|
CheckerContext &C) const {
|
2010-02-09 00:18:51 +08:00
|
|
|
if (C.getState()->getSVal(A->getIdx()).isUndef()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2009-11-11 21:42:54 +08:00
|
|
|
if (!BT)
|
2011-02-28 09:27:41 +08:00
|
|
|
BT.reset(new BuiltinBug("Array subscript is undefined"));
|
2009-11-11 21:42:54 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2009-11-14 20:08:24 +08:00
|
|
|
EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
|
2009-11-11 21:42:54 +08:00
|
|
|
R->addRange(A->getIdx()->getSourceRange());
|
|
|
|
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
|
|
|
|
A->getIdx());
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:27:41 +08:00
|
|
|
|
|
|
|
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedArraySubscriptChecker>();
|
|
|
|
}
|