}
const IdentifierInfo *II = FD->getIdentifier();
- if (!II)
- return getDefaultSummary();
- StringRef FName = II->getName();
+ StringRef FName = II ? II->getName() : "";
// Strip away preceding '_'. Doing this here will effect all the checks
// down below.
if (FName == "retain")
return getOSSummaryRetainRule(FD);
+
+ if (FName == "free")
+ return getOSSummaryFreeRule(FD);
+
+ if (MD->getOverloadedOperator() == OO_New)
+ return getOSSummaryCreateRule(MD);
}
}
const RetainSummary *Summ;
switch (Call.getKind()) {
case CE_Function:
- Summ = getFunctionSummary(cast<SimpleFunctionCall>(Call).getDecl());
- break;
case CE_CXXMember:
- Summ = getFunctionSummary(cast<CXXMemberCall>(Call).getDecl());
- break;
case CE_CXXMemberOperator:
- Summ = getFunctionSummary(cast<CXXMemberOperatorCall>(Call).getDecl());
- break;
case CE_CXXConstructor:
- Summ = getFunctionSummary(cast<CXXConstructorCall>(Call).getDecl());
+ case CE_CXXAllocator:
+ Summ = getFunctionSummary(cast_or_null<FunctionDecl>(Call.getDecl()));
break;
case CE_Block:
case CE_CXXDestructor:
- case CE_CXXAllocator:
// FIXME: These calls are currently unsupported.
return getPersistentStopSummary();
case CE_ObjCMessage: {
/*ThisEff=*/DecRef);
}
+const RetainSummary *
+RetainSummaryManager::getOSSummaryFreeRule(const FunctionDecl *FD) {
+ return getPersistentSummary(RetEffect::MakeNoRet(),
+ /*ReceiverEff=*/DoNothing,
+ /*DefaultEff=*/DoNothing,
+ /*ThisEff=*/Dealloc);
+}
+
const RetainSummary *
RetainSummaryManager::getOSSummaryCreateRule(const FunctionDecl *FD) {
return getPersistentSummary(RetEffect::MakeOwned(RetEffect::OS));
#define OSDynamicCast(type, inst) \
((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
+using size_t = decltype(sizeof(int));
+
struct OSObject {
virtual void retain();
virtual void release() {};
+ virtual void free();
virtual ~OSObject(){}
unsigned int foo() { return 42; }
static OSObject *getObject();
static OSObject *GetObject();
+
+ static void * operator new(size_t size);
+
static const OSMetaClass * const metaClass;
};
static OSObject *safeMetaCast(const OSObject *inst, const OSMetaClass *meta);
};
+void check_free_no_error() {
+ OSArray *arr = OSArray::withCapacity(10);
+ arr->retain();
+ arr->retain();
+ arr->retain();
+ arr->free();
+}
+
+void check_free_use_after_free() {
+ OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type OSArray with a +1 retain count}}
+ arr->retain(); // expected-note{{Reference count incremented. The object now has a +2 retain count}}
+ arr->free(); // expected-note{{Object released}}
+ arr->retain(); // expected-warning{{Reference-counted object is used after it is released}}
+ // expected-note@-1{{Reference-counted object is used after it is released}}
+}
+
+unsigned int check_leak_explicit_new() {
+ OSArray *arr = new OSArray; // expected-note{{Operator new returns an OSObject of type OSArray with a +1 retain count}}
+ return arr->getCount(); // expected-note{{Object leaked: allocated object of type OSArray is not referenced later in this execution path and has a retain count of +1}}
+ // expected-warning@-1{{Potential leak of an object of type OSArray}}
+}
+
+unsigned int check_leak_factory() {
+ OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to method 'OSArray::withCapacity' returns an OSObject of type OSArray with a +1 retain count}}
+ return arr->getCount(); // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}}
+ // expected-warning@-1{{Potential leak of an object stored into 'arr'}}
+}
+
void check_get_object() {
OSObject::getObject();
}