diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-03-31 01:14:06 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-03-31 01:14:06 +0000 |
commit | d786f1a6331246e7d0eedde277f126ab92089eeb (patch) | |
tree | 69e9b9748978d88f64616402b73fb7805ca67f15 /tools/libclang/IndexingContext.cpp | |
parent | 86cff6080ba1db907009885adb5c6c64007e8b48 (diff) |
[libclang] Fix use-after-free bug when handling attributes indexing info.
When indexing a property with a getter/setter with attributes, the allocated memory
for AttrListInfo could get released before its destructor is run.
Fixes rdar://11113442.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153792 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang/IndexingContext.cpp')
-rw-r--r-- | tools/libclang/IndexingContext.cpp | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/tools/libclang/IndexingContext.cpp b/tools/libclang/IndexingContext.cpp index c9150b9a5d..ace5c75a96 100644 --- a/tools/libclang/IndexingContext.cpp +++ b/tools/libclang/IndexingContext.cpp @@ -61,9 +61,9 @@ IBOutletCollectionInfo::IBOutletCollectionInfo( IBCollInfo.objcClass = 0; } -AttrListInfo::AttrListInfo(const Decl *D, - IndexingContext &IdxCtx, - ScratchAlloc &SA) : ref_cnt(0) { +AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx) + : SA(IdxCtx), ref_cnt(0) { + if (!D->hasAttrs()) return; @@ -113,19 +113,11 @@ AttrListInfo::AttrListInfo(const Decl *D, CXAttrs.push_back(&Attrs[i]); } -AttrListInfo::AttrListInfo(const AttrListInfo &other) { - assert(other.ref_cnt == 0 && - "Should not copy an AttrListInfo that is ref-counted"); - ref_cnt = 0; - - Attrs = other.Attrs; - IBCollAttrs = other.IBCollAttrs; - - for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) - CXAttrs.push_back(&IBCollAttrs[i]); - - for (unsigned i = 0, e = Attrs.size(); i != e; ++i) - CXAttrs.push_back(&Attrs[i]); +IntrusiveRefCntPtr<AttrListInfo> +AttrListInfo::create(const Decl *D, IndexingContext &IdxCtx) { + ScratchAlloc SA(IdxCtx); + AttrListInfo *attrs = SA.allocate<AttrListInfo>(); + return new (attrs) AttrListInfo(D, IdxCtx); } IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, @@ -281,9 +273,8 @@ bool IndexingContext::handleDecl(const NamedDecl *D, DInfo.loc = getIndexLoc(Loc); DInfo.isImplicit = D->isImplicit(); - AttrListInfo AttrList(D, *this, SA); - DInfo.attributes = AttrList.getAttrs(); - DInfo.numAttributes = AttrList.getNumAttrs(); + DInfo.attributes = DInfo.EntInfo.attributes; + DInfo.numAttributes = DInfo.EntInfo.numAttributes; getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer); DInfo.semanticContainer = &DInfo.SemanticContainer; @@ -443,9 +434,10 @@ bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { } bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { + ScratchAlloc SA(*this); + ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); EntityInfo ClassEntity; - ScratchAlloc SA(*this); const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); SourceLocation ClassLoc = D->getLocation(); SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc @@ -474,10 +466,11 @@ bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { } bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { + ScratchAlloc SA(*this); + const ObjCCategoryDecl *CatD = D->getCategoryDecl(); ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); EntityInfo ClassEntity; - ScratchAlloc SA(*this); const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); SourceLocation ClassLoc = D->getLocation(); SourceLocation CategoryLoc = D->getCategoryNameLoc(); @@ -522,10 +515,11 @@ bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, } bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { + ScratchAlloc SA(*this); + ObjCPropertyDeclInfo DInfo; EntityInfo GetterEntity; EntityInfo SetterEntity; - ScratchAlloc SA(*this); DInfo.ObjCPropDeclInfo.declInfo = &DInfo; @@ -846,11 +840,9 @@ void IndexingContext::getEntityInfo(const NamedDecl *D, EntityInfo.lang = CXIdxEntityLang_C; if (D->hasAttrs()) { - AttrListInfo *attrs = SA.allocate<AttrListInfo>(); - new (attrs) AttrListInfo(D, *this, SA); - EntityInfo.AttrList = attrs; - EntityInfo.attributes = attrs->getAttrs(); - EntityInfo.numAttributes = attrs->getNumAttrs(); + EntityInfo.AttrList = AttrListInfo::create(D, *this); + EntityInfo.attributes = EntityInfo.AttrList->getAttrs(); + EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs(); } if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |