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> > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT;
|
2011-02-28 09:27:41 +08:00
|
|
|
|
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 {
|
2012-01-07 06:09:28 +08:00
|
|
|
if (C.getState()->getSVal(A->getIdx(), C.getLocationContext()).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.
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT, BT->getName(), N);
|
2009-11-11 21:42:54 +08:00
|
|
|
R->addRange(A->getIdx()->getSourceRange());
|
2011-08-20 06:33:38 +08:00
|
|
|
R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
|
2012-03-09 09:13:14 +08:00
|
|
|
A->getIdx(),
|
|
|
|
R));
|
2009-11-11 21:42:54 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:27:41 +08:00
|
|
|
|
|
|
|
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedArraySubscriptChecker>();
|
|
|
|
}
|