2009-11-04 09:43:07 +08:00
|
|
|
//=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===//
|
2009-11-03 20:13:38 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-11-07 11:56:57 +08:00
|
|
|
// This defines VLASizeChecker, a builtin check in GRExprEngine that
|
2009-11-04 09:43:07 +08:00
|
|
|
// performs checks for declaration of VLA of undefined or zero size.
|
2009-11-03 20:13:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-07 05:51:50 +08:00
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2009-11-07 11:56:57 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
|
2009-11-03 20:13:38 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-11-07 05:51:50 +08:00
|
|
|
namespace {
|
2009-11-07 11:56:57 +08:00
|
|
|
class VISIBILITY_HIDDEN VLASizeChecker : public CheckerVisitor<VLASizeChecker> {
|
|
|
|
BugType *BT_zero;
|
|
|
|
BugType *BT_undef;
|
2009-11-07 05:51:50 +08:00
|
|
|
|
|
|
|
public:
|
2009-11-07 11:56:57 +08:00
|
|
|
VLASizeChecker() : BT_zero(0), BT_undef(0) {}
|
|
|
|
static void *getTag() { static int tag = 0; return &tag; }
|
|
|
|
void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS);
|
2009-11-07 05:51:50 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void clang::RegisterVLASizeChecker(GRExprEngine &Eng) {
|
2009-11-07 11:56:57 +08:00
|
|
|
Eng.registerCheck(new VLASizeChecker());
|
2009-11-04 09:43:07 +08:00
|
|
|
}
|
|
|
|
|
2009-11-07 11:56:57 +08:00
|
|
|
void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
|
|
|
|
if (!DS->isSingleDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
if (!VD)
|
|
|
|
return;
|
|
|
|
|
2009-11-10 06:08:55 +08:00
|
|
|
const VariableArrayType *VLA
|
|
|
|
= C.getASTContext().getAsVariableArrayType(VD->getType());
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!VLA)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Handle multi-dimensional VLAs.
|
|
|
|
const Expr* SE = VLA->getSizeExpr();
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
SVal sizeV = state->getSVal(SE);
|
|
|
|
|
|
|
|
if (sizeV.isUndef()) {
|
|
|
|
// Generate an error node.
|
2009-11-24 06:22:01 +08:00
|
|
|
ExplodedNode *N = C.GenerateSink();
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT_undef)
|
|
|
|
BT_undef = new BuiltinBug("Declared variable-length array (VLA) uses a "
|
|
|
|
"garbage value as its size");
|
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new EnhancedBugReport(*BT_undef, BT_undef->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
|
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
2009-11-04 09:43:07 +08:00
|
|
|
}
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
// Check if the size is zero.
|
|
|
|
DefinedOrUnknownSVal sizeD = cast<DefinedOrUnknownSVal>(sizeV);
|
|
|
|
|
|
|
|
const GRState *stateNotZero, *stateZero;
|
|
|
|
llvm::tie(stateNotZero, stateZero) = state->Assume(sizeD);
|
|
|
|
|
|
|
|
if (stateZero && !stateNotZero) {
|
2009-11-24 06:22:01 +08:00
|
|
|
ExplodedNode* N = C.GenerateSink(stateZero);
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!BT_zero)
|
|
|
|
BT_zero = new BuiltinBug("Declared variable-length array (VLA) has zero "
|
|
|
|
"size");
|
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new EnhancedBugReport(*BT_zero, BT_zero->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
|
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
2009-11-03 20:13:38 +08:00
|
|
|
}
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
// From this point on, assume that the size is not zero.
|
2009-11-24 06:22:01 +08:00
|
|
|
C.addTransition(stateNotZero);
|
2009-11-03 20:13:38 +08:00
|
|
|
}
|