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"
|
2013-06-17 20:56:08 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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"
|
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> > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<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 {
|
2013-04-03 09:39:08 +08:00
|
|
|
const Expr *Index = A->getIdx();
|
|
|
|
if (!C.getSVal(Index).isUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Sema generates anonymous array variables for copying array struct fields.
|
|
|
|
// Don't warn if we're in an implicitly-generated constructor.
|
|
|
|
const Decl *D = C.getLocationContext()->getDecl();
|
|
|
|
if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D))
|
2013-08-08 00:16:48 +08:00
|
|
|
if (Ctor->isDefaulted())
|
2013-04-03 09:39:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ExplodedNode *N = C.generateSink();
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(this, "Array subscript is undefined"));
|
2013-04-03 09:39:08 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
BugReport *R = new BugReport(*BT, BT->getName(), N);
|
|
|
|
R->addRange(A->getIdx()->getSourceRange());
|
|
|
|
bugreporter::trackNullOrUndefValue(N, A->getIdx(), *R);
|
|
|
|
C.emitReport(R);
|
2009-11-11 21:42:54 +08:00
|
|
|
}
|
2011-02-28 09:27:41 +08:00
|
|
|
|
|
|
|
void ento::registerUndefinedArraySubscriptChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedArraySubscriptChecker>();
|
|
|
|
}
|