2008-03-27 10:25:52 +03:00
|
|
|
//== BasicObjCFoundationChecks.cpp - Simple Apple-Foundation checks -*- C++ -*--
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines BasicObjCFoundationChecks, a class that encapsulates
|
|
|
|
// a set of simple checks to run on Objective-C code using Apple's Foundation
|
|
|
|
// classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-27 20:17:22 +03:00
|
|
|
#include "BasicObjCFoundationChecks.h"
|
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRSimpleAPICheck.h"
|
2008-07-22 20:21:24 +04:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2008-08-13 08:27:00 +04:00
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-04-03 21:57:38 +04:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-10-04 09:50:14 +04:00
|
|
|
#include "clang/Analysis/PathSensitive/MemRegion.h"
|
2008-03-27 10:25:52 +03:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-07-22 20:21:24 +04:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2008-08-11 09:35:13 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-03-27 10:25:52 +03:00
|
|
|
#include "clang/AST/Expr.h"
|
2008-05-30 01:12:08 +04:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2008-03-27 10:25:52 +03:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2008-04-03 21:57:38 +04:00
|
|
|
|
|
|
|
static ObjCInterfaceType* GetReceiverType(ObjCMessageExpr* ME) {
|
|
|
|
Expr* Receiver = ME->getReceiver();
|
|
|
|
|
|
|
|
if (!Receiver)
|
|
|
|
return NULL;
|
|
|
|
|
2008-04-04 01:44:24 +04:00
|
|
|
QualType X = Receiver->getType();
|
2008-04-19 23:12:50 +04:00
|
|
|
|
2008-05-01 02:48:21 +04:00
|
|
|
if (X->isPointerType()) {
|
|
|
|
Type* TP = X.getTypePtr();
|
|
|
|
const PointerType* T = TP->getAsPointerType();
|
|
|
|
return dyn_cast<ObjCInterfaceType>(T->getPointeeType().getTypePtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Support ObjCQualifiedIdType?
|
|
|
|
return NULL;
|
2008-04-03 21:57:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* GetReceiverNameType(ObjCMessageExpr* ME) {
|
|
|
|
ObjCInterfaceType* ReceiverType = GetReceiverType(ME);
|
|
|
|
return ReceiverType ? ReceiverType->getDecl()->getIdentifier()->getName()
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
namespace {
|
2008-09-21 23:01:39 +04:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
class VISIBILITY_HIDDEN APIMisuse : public BugType {
|
2008-04-03 21:57:38 +04:00
|
|
|
public:
|
2009-02-05 02:49:09 +03:00
|
|
|
APIMisuse(const char* name) : BugType(name, "API Misuse (Apple)") {}
|
2008-04-03 21:57:38 +04:00
|
|
|
};
|
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
class VISIBILITY_HIDDEN BasicObjCFoundationChecks : public GRSimpleAPICheck {
|
2009-02-05 02:49:09 +03:00
|
|
|
APIMisuse *BT;
|
|
|
|
BugReporter& BR;
|
2008-03-28 00:15:17 +03:00
|
|
|
ASTContext &Ctx;
|
2008-08-13 08:27:00 +04:00
|
|
|
GRStateManager* VMgr;
|
2009-02-05 02:49:09 +03:00
|
|
|
|
2008-10-17 09:57:07 +04:00
|
|
|
SVal GetSVal(const GRState* St, Expr* E) { return VMgr->GetSVal(St, E); }
|
2008-03-27 10:25:52 +03:00
|
|
|
|
2008-03-28 00:15:17 +03:00
|
|
|
bool isNSString(ObjCInterfaceType* T, const char* suffix);
|
|
|
|
bool AuditNSString(NodeTy* N, ObjCMessageExpr* ME);
|
2008-03-27 10:25:52 +03:00
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
void Warn(NodeTy* N, Expr* E, const std::string& s);
|
|
|
|
void WarnNilArg(NodeTy* N, Expr* E);
|
|
|
|
|
|
|
|
bool CheckNilArg(NodeTy* N, unsigned Arg);
|
2008-03-27 10:25:52 +03:00
|
|
|
|
|
|
|
public:
|
2009-02-05 02:49:09 +03:00
|
|
|
BasicObjCFoundationChecks(ASTContext& ctx, GRStateManager* vmgr,
|
|
|
|
BugReporter& br)
|
|
|
|
: BT(0), BR(br), Ctx(ctx), VMgr(vmgr) {}
|
|
|
|
|
|
|
|
bool Audit(ExplodedNode<GRState>* N, GRStateManager&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void WarnNilArg(NodeTy* N, ObjCMessageExpr* ME, unsigned Arg) {
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
|
|
|
os << "Argument to '" << GetReceiverNameType(ME) << "' method '"
|
|
|
|
<< ME->getSelector().getAsString() << "' cannot be nil.";
|
|
|
|
|
|
|
|
// Lazily create the BugType object for NilArg. This will be owned
|
|
|
|
// by the BugReporter object 'BR' once we call BR.EmitWarning.
|
|
|
|
if (!BT) BT = new APIMisuse("nil argument");
|
|
|
|
|
|
|
|
RangedBugReport *R = new RangedBugReport(*BT, os.str().c_str(), N);
|
|
|
|
R->addRange(ME->getArg(Arg)->getSourceRange());
|
|
|
|
BR.EmitReport(R);
|
2008-04-03 21:57:38 +04:00
|
|
|
}
|
2008-03-27 10:25:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2008-03-27 20:17:22 +03:00
|
|
|
GRSimpleAPICheck*
|
|
|
|
clang::CreateBasicObjCFoundationChecks(ASTContext& Ctx,
|
2009-02-05 02:49:09 +03:00
|
|
|
GRStateManager* VMgr, BugReporter& BR) {
|
2008-03-27 20:17:22 +03:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
return new BasicObjCFoundationChecks(Ctx, VMgr, BR);
|
2008-03-27 20:17:22 +03:00
|
|
|
}
|
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
|
2008-08-13 08:27:00 +04:00
|
|
|
bool BasicObjCFoundationChecks::Audit(ExplodedNode<GRState>* N,
|
|
|
|
GRStateManager&) {
|
2008-03-28 01:05:32 +03:00
|
|
|
|
|
|
|
ObjCMessageExpr* ME =
|
|
|
|
cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
ObjCInterfaceType* ReceiverType = GetReceiverType(ME);
|
2008-03-27 10:25:52 +03:00
|
|
|
|
|
|
|
if (!ReceiverType)
|
2008-05-20 21:33:56 +04:00
|
|
|
return false;
|
2008-03-28 01:05:32 +03:00
|
|
|
|
|
|
|
const char* name = ReceiverType->getDecl()->getIdentifier()->getName();
|
2008-03-27 10:25:52 +03:00
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
if (!name)
|
|
|
|
return false;
|
2008-03-27 10:25:52 +03:00
|
|
|
|
|
|
|
if (name[0] != 'N' || name[1] != 'S')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
name += 2;
|
|
|
|
|
|
|
|
// FIXME: Make all of this faster.
|
|
|
|
|
|
|
|
if (isNSString(ReceiverType, name))
|
|
|
|
return AuditNSString(N, ME);
|
|
|
|
|
2008-05-20 21:33:56 +04:00
|
|
|
return false;
|
2008-03-27 10:25:52 +03:00
|
|
|
}
|
|
|
|
|
2008-10-17 09:57:07 +04:00
|
|
|
static inline bool isNil(SVal X) {
|
|
|
|
return isa<loc::ConcreteInt>(X);
|
2008-03-28 00:15:17 +03:00
|
|
|
}
|
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
bool BasicObjCFoundationChecks::CheckNilArg(NodeTy* N, unsigned Arg) {
|
|
|
|
ObjCMessageExpr* ME =
|
|
|
|
cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
Expr * E = ME->getArg(Arg);
|
|
|
|
|
2008-10-17 09:57:07 +04:00
|
|
|
if (isNil(GetSVal(N->getState(), E))) {
|
2008-04-03 21:57:38 +04:00
|
|
|
WarnNilArg(N, ME, Arg);
|
2008-03-28 01:05:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-27 10:25:52 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NSString checking.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool BasicObjCFoundationChecks::isNSString(ObjCInterfaceType* T,
|
|
|
|
const char* suffix) {
|
|
|
|
|
|
|
|
return !strcmp("String", suffix) || !strcmp("MutableString", suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicObjCFoundationChecks::AuditNSString(NodeTy* N,
|
|
|
|
ObjCMessageExpr* ME) {
|
|
|
|
|
|
|
|
Selector S = ME->getSelector();
|
|
|
|
|
|
|
|
if (S.isUnarySelector())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: This is going to be really slow doing these checks with
|
|
|
|
// lexical comparisons.
|
|
|
|
|
2008-11-24 06:33:13 +03:00
|
|
|
std::string name = S.getAsString();
|
2008-03-28 00:23:57 +03:00
|
|
|
assert (!name.empty());
|
|
|
|
const char* cstr = &name[0];
|
|
|
|
unsigned len = name.size();
|
2008-03-28 01:05:32 +03:00
|
|
|
|
2008-03-28 00:23:57 +03:00
|
|
|
switch (len) {
|
|
|
|
default:
|
|
|
|
break;
|
2008-03-28 19:09:38 +03:00
|
|
|
case 8:
|
2008-03-28 01:05:32 +03:00
|
|
|
if (!strcmp(cstr, "compare:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
2008-03-28 19:09:38 +03:00
|
|
|
|
|
|
|
case 15:
|
|
|
|
// FIXME: Checking for initWithFormat: will not work in most cases
|
|
|
|
// yet because [NSString alloc] returns id, not NSString*. We will
|
|
|
|
// need support for tracking expected-type information in the analyzer
|
|
|
|
// to find these errors.
|
|
|
|
if (!strcmp(cstr, "initWithFormat:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
2008-03-27 10:25:52 +03:00
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
case 16:
|
|
|
|
if (!strcmp(cstr, "compare:options:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 22:
|
|
|
|
if (!strcmp(cstr, "compare:options:range:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 23:
|
|
|
|
|
|
|
|
if (!strcmp(cstr, "caseInsensitiveCompare:"))
|
|
|
|
return CheckNilArg(N, 0);
|
2008-03-28 00:23:57 +03:00
|
|
|
|
|
|
|
break;
|
2008-03-28 19:09:38 +03:00
|
|
|
|
2008-03-28 01:05:32 +03:00
|
|
|
case 29:
|
|
|
|
if (!strcmp(cstr, "compare:options:range:locale:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 37:
|
|
|
|
if (!strcmp(cstr, "componentsSeparatedByCharactersInSet:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
2008-03-27 10:25:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN AuditCFNumberCreate : public GRSimpleAPICheck {
|
2009-02-05 02:49:09 +03:00
|
|
|
APIMisuse* BT;
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
// FIXME: Either this should be refactored into GRSimpleAPICheck, or
|
|
|
|
// it should always be passed with a call to Audit. The latter
|
|
|
|
// approach makes this class more stateless.
|
|
|
|
ASTContext& Ctx;
|
|
|
|
IdentifierInfo* II;
|
2008-08-13 08:27:00 +04:00
|
|
|
GRStateManager* VMgr;
|
2009-02-05 02:49:09 +03:00
|
|
|
BugReporter& BR;
|
2008-06-27 03:59:48 +04:00
|
|
|
|
2008-10-17 09:57:07 +04:00
|
|
|
SVal GetSVal(const GRState* St, Expr* E) { return VMgr->GetSVal(St, E); }
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
public:
|
2009-02-05 02:49:09 +03:00
|
|
|
AuditCFNumberCreate(ASTContext& ctx, GRStateManager* vmgr, BugReporter& br)
|
|
|
|
: BT(0), Ctx(ctx), II(&Ctx.Idents.get("CFNumberCreate")), VMgr(vmgr), BR(br){}
|
2008-06-27 03:59:48 +04:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
~AuditCFNumberCreate() {}
|
2008-06-27 03:59:48 +04:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
bool Audit(ExplodedNode<GRState>* N, GRStateManager&);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
private:
|
2008-10-18 00:28:54 +04:00
|
|
|
void AddError(const TypedRegion* R, Expr* Ex, ExplodedNode<GRState> *N,
|
2008-06-27 03:59:48 +04:00
|
|
|
uint64_t SourceSize, uint64_t TargetSize, uint64_t NumberKind);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
enum CFNumberType {
|
|
|
|
kCFNumberSInt8Type = 1,
|
|
|
|
kCFNumberSInt16Type = 2,
|
|
|
|
kCFNumberSInt32Type = 3,
|
|
|
|
kCFNumberSInt64Type = 4,
|
|
|
|
kCFNumberFloat32Type = 5,
|
|
|
|
kCFNumberFloat64Type = 6,
|
|
|
|
kCFNumberCharType = 7,
|
|
|
|
kCFNumberShortType = 8,
|
|
|
|
kCFNumberIntType = 9,
|
|
|
|
kCFNumberLongType = 10,
|
|
|
|
kCFNumberLongLongType = 11,
|
|
|
|
kCFNumberFloatType = 12,
|
|
|
|
kCFNumberDoubleType = 13,
|
|
|
|
kCFNumberCFIndexType = 14,
|
|
|
|
kCFNumberNSIntegerType = 15,
|
|
|
|
kCFNumberCGFloatType = 16
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template<typename T>
|
|
|
|
class Optional {
|
|
|
|
bool IsKnown;
|
|
|
|
T Val;
|
|
|
|
public:
|
|
|
|
Optional() : IsKnown(false), Val(0) {}
|
|
|
|
Optional(const T& val) : IsKnown(true), Val(val) {}
|
|
|
|
|
|
|
|
bool isKnown() const { return IsKnown; }
|
|
|
|
|
|
|
|
const T& getValue() const {
|
|
|
|
assert (isKnown());
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T&() const {
|
|
|
|
return getValue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<uint64_t> GetCFNumberSize(ASTContext& Ctx, uint64_t i) {
|
|
|
|
static unsigned char FixedSize[] = { 8, 16, 32, 64, 32, 64 };
|
|
|
|
|
|
|
|
if (i < kCFNumberCharType)
|
|
|
|
return FixedSize[i-1];
|
|
|
|
|
|
|
|
QualType T;
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case kCFNumberCharType: T = Ctx.CharTy; break;
|
|
|
|
case kCFNumberShortType: T = Ctx.ShortTy; break;
|
|
|
|
case kCFNumberIntType: T = Ctx.IntTy; break;
|
|
|
|
case kCFNumberLongType: T = Ctx.LongTy; break;
|
|
|
|
case kCFNumberLongLongType: T = Ctx.LongLongTy; break;
|
|
|
|
case kCFNumberFloatType: T = Ctx.FloatTy; break;
|
|
|
|
case kCFNumberDoubleType: T = Ctx.DoubleTy; break;
|
|
|
|
case kCFNumberCFIndexType:
|
|
|
|
case kCFNumberNSIntegerType:
|
|
|
|
case kCFNumberCGFloatType:
|
|
|
|
// FIXME: We need a way to map from names to Type*.
|
|
|
|
default:
|
|
|
|
return Optional<uint64_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ctx.getTypeSize(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static const char* GetCFNumberTypeStr(uint64_t i) {
|
|
|
|
static const char* Names[] = {
|
|
|
|
"kCFNumberSInt8Type",
|
|
|
|
"kCFNumberSInt16Type",
|
|
|
|
"kCFNumberSInt32Type",
|
|
|
|
"kCFNumberSInt64Type",
|
|
|
|
"kCFNumberFloat32Type",
|
|
|
|
"kCFNumberFloat64Type",
|
|
|
|
"kCFNumberCharType",
|
|
|
|
"kCFNumberShortType",
|
|
|
|
"kCFNumberIntType",
|
|
|
|
"kCFNumberLongType",
|
|
|
|
"kCFNumberLongLongType",
|
|
|
|
"kCFNumberFloatType",
|
|
|
|
"kCFNumberDoubleType",
|
|
|
|
"kCFNumberCFIndexType",
|
|
|
|
"kCFNumberNSIntegerType",
|
|
|
|
"kCFNumberCGFloatType"
|
|
|
|
};
|
|
|
|
|
|
|
|
return i <= kCFNumberCGFloatType ? Names[i-1] : "Invalid CFNumberType";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-13 08:27:00 +04:00
|
|
|
bool AuditCFNumberCreate::Audit(ExplodedNode<GRState>* N,GRStateManager&){
|
2008-06-27 03:59:48 +04:00
|
|
|
CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
Expr* Callee = CE->getCallee();
|
2008-10-17 09:57:07 +04:00
|
|
|
SVal CallV = GetSVal(N->getState(), Callee);
|
|
|
|
loc::FuncVal* FuncV = dyn_cast<loc::FuncVal>(&CallV);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
if (!FuncV || FuncV->getDecl()->getIdentifier() != II || CE->getNumArgs()!=3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the value of the "theType" argument.
|
2008-10-17 09:57:07 +04:00
|
|
|
SVal TheTypeVal = GetSVal(N->getState(), CE->getArg(1));
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
// FIXME: We really should allow ranges of valid theType values, and
|
|
|
|
// bifurcate the state appropriately.
|
2008-10-17 09:57:07 +04:00
|
|
|
nonloc::ConcreteInt* V = dyn_cast<nonloc::ConcreteInt>(&TheTypeVal);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
if (!V)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t NumberKind = V->getValue().getLimitedValue();
|
|
|
|
Optional<uint64_t> TargetSize = GetCFNumberSize(Ctx, NumberKind);
|
|
|
|
|
|
|
|
// FIXME: In some cases we can emit an error.
|
|
|
|
if (!TargetSize.isKnown())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Look at the value of the integer being passed by reference. Essentially
|
|
|
|
// we want to catch cases where the value passed in is not equal to the
|
|
|
|
// size of the type being created.
|
2008-10-17 09:57:07 +04:00
|
|
|
SVal TheValueExpr = GetSVal(N->getState(), CE->getArg(2));
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
// FIXME: Eventually we should handle arbitrary locations. We can do this
|
|
|
|
// by having an enhanced memory model that does low-level typing.
|
2008-10-17 09:57:07 +04:00
|
|
|
loc::MemRegionVal* LV = dyn_cast<loc::MemRegionVal>(&TheValueExpr);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
if (!LV)
|
|
|
|
return false;
|
|
|
|
|
2008-10-18 00:28:54 +04:00
|
|
|
const TypedRegion* R = dyn_cast<TypedRegion>(LV->getRegion());
|
2008-12-14 00:49:13 +03:00
|
|
|
if (!R) return false;
|
2008-10-04 09:50:14 +04:00
|
|
|
|
2008-12-14 00:49:13 +03:00
|
|
|
while (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
|
|
|
|
R = dyn_cast<TypedRegion>(ATR->getSuperRegion());
|
|
|
|
if (!R) return false;
|
|
|
|
}
|
2008-10-04 09:50:14 +04:00
|
|
|
|
2008-12-14 00:49:13 +03:00
|
|
|
QualType T = Ctx.getCanonicalType(R->getRValueType(Ctx));
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
// FIXME: If the pointee isn't an integer type, should we flag a warning?
|
|
|
|
// People can do weird stuff with pointers.
|
|
|
|
|
|
|
|
if (!T->isIntegerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t SourceSize = Ctx.getTypeSize(T);
|
|
|
|
|
|
|
|
// CHECK: is SourceSize == TargetSize
|
|
|
|
|
|
|
|
if (SourceSize == TargetSize)
|
|
|
|
return false;
|
|
|
|
|
2008-10-04 09:50:14 +04:00
|
|
|
AddError(R, CE->getArg(2), N, SourceSize, TargetSize, NumberKind);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
// FIXME: We can actually create an abstract "CFNumber" object that has
|
|
|
|
// the bits initialized to the provided values.
|
|
|
|
return SourceSize < TargetSize;
|
|
|
|
}
|
|
|
|
|
2008-10-18 00:28:54 +04:00
|
|
|
void AuditCFNumberCreate::AddError(const TypedRegion* R, Expr* Ex,
|
2008-08-13 08:27:00 +04:00
|
|
|
ExplodedNode<GRState> *N,
|
2008-06-27 03:59:48 +04:00
|
|
|
uint64_t SourceSize, uint64_t TargetSize,
|
|
|
|
uint64_t NumberKind) {
|
2009-02-05 02:49:09 +03:00
|
|
|
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2008-06-27 03:59:48 +04:00
|
|
|
|
|
|
|
os << (SourceSize == 8 ? "An " : "A ")
|
|
|
|
<< SourceSize << " bit integer is used to initialize a CFNumber "
|
|
|
|
"object that represents "
|
|
|
|
<< (TargetSize == 8 ? "an " : "a ")
|
|
|
|
<< TargetSize << " bit integer. ";
|
|
|
|
|
|
|
|
if (SourceSize < TargetSize)
|
|
|
|
os << (TargetSize - SourceSize)
|
|
|
|
<< " bits of the CFNumber value will be garbage." ;
|
|
|
|
else
|
|
|
|
os << (SourceSize - TargetSize)
|
|
|
|
<< " bits of the input integer will be lost.";
|
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
// Lazily create the BugType object. This will be owned
|
|
|
|
// by the BugReporter object 'BR' once we call BR.EmitWarning.
|
|
|
|
if (!BT) BT = new APIMisuse("Bad use of CFNumberCreate");
|
|
|
|
RangedBugReport *report = new RangedBugReport(*BT, os.str().c_str(), N);
|
|
|
|
report->addRange(Ex->getSourceRange());
|
|
|
|
BR.EmitReport(report);
|
2008-06-27 03:59:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GRSimpleAPICheck*
|
|
|
|
clang::CreateAuditCFNumberCreate(ASTContext& Ctx,
|
2009-02-05 02:49:09 +03:00
|
|
|
GRStateManager* VMgr, BugReporter& BR) {
|
|
|
|
return new AuditCFNumberCreate(Ctx, VMgr, BR);
|
2008-06-27 03:59:48 +04:00
|
|
|
}
|
|
|
|
|
2008-07-22 20:21:24 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check registration.
|
|
|
|
|
|
|
|
void clang::RegisterAppleChecks(GRExprEngine& Eng) {
|
|
|
|
ASTContext& Ctx = Eng.getContext();
|
2008-08-13 08:27:00 +04:00
|
|
|
GRStateManager* VMgr = &Eng.getStateManager();
|
2009-02-05 02:49:09 +03:00
|
|
|
BugReporter &BR = Eng.getBugReporter();
|
2008-07-22 20:21:24 +04:00
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
Eng.AddCheck(CreateBasicObjCFoundationChecks(Ctx, VMgr, BR),
|
2008-07-22 20:21:24 +04:00
|
|
|
Stmt::ObjCMessageExprClass);
|
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
Eng.AddCheck(CreateAuditCFNumberCreate(Ctx, VMgr, BR),
|
2008-09-19 01:25:13 +04:00
|
|
|
Stmt::CallExprClass);
|
|
|
|
|
2009-02-05 02:49:09 +03:00
|
|
|
RegisterNSErrorChecks(BR, Eng);
|
2008-07-22 20:21:24 +04:00
|
|
|
}
|