[cfe-commits] r70693 - /cfe/trunk/lib/AST/ASTContext.cpp

Daniel Dunbar daniel at zuster.org
Sun May 3 04:41:52 PDT 2009


Author: ddunbar
Date: Sun May  3 06:41:43 2009
New Revision: 70693

URL: http://llvm.org/viewvc/llvm-project?rev=70693&view=rev
Log:
Fix a possible memory error, the record layout entry could be
invalidated by layout out the super class, we cannot cache the map
entry.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=70693&r1=70692&r2=70693&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sun May  3 06:41:43 2009
@@ -745,9 +745,10 @@
 ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
                           const ObjCImplementationDecl *Impl) {
   // Look up this layout, if already laid out, return what we have.
-  const ASTRecordLayout *&Entry = 
-    ObjCLayouts[Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D];
-  if (Entry) return *Entry;
+  ObjCContainerDecl *Key = 
+    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
+  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
+    return *Entry;
 
   unsigned FieldCount = D->ivar_size();
   // Add in synthesized ivar count if laying out an implementation.
@@ -757,7 +758,7 @@
       if ((*I)->getPropertyIvarDecl())
         ++FieldCount;
 
-    // If there aren't any sythesized ivar's then reuse the interface
+    // If there aren't any sythesized ivars then reuse the interface
     // entry. Note we can't cache this because we simply free all
     // entries later; however we shouldn't look up implementations
     // frequently.
@@ -765,23 +766,21 @@
       return getObjCLayout(D, 0);
   }
 
-  // Allocate and assign into ASTRecordLayouts here.  The "Entry" reference can
-  // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
   ASTRecordLayout *NewEntry = NULL;
   if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
     FieldCount++;
     const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
     unsigned Alignment = SL.getAlignment();
     uint64_t Size = SL.getSize();
-    NewEntry = new ASTRecordLayout(Size, Alignment);
+
+    ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
     NewEntry->InitializeLayout(FieldCount);
     // Super class is at the beginning of the layout.
     NewEntry->SetFieldOffset(0, 0);
   } else {
-    NewEntry = new ASTRecordLayout();
+    ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
     NewEntry->InitializeLayout(FieldCount);
   }
-  Entry = NewEntry;
 
   unsigned StructPacking = 0;
   if (const PackedAttr *PA = D->getAttr<PackedAttr>())





More information about the cfe-commits mailing list