[cfe-commits] r109505 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp

Douglas Gregor dgregor at apple.com
Tue Jul 27 07:52:07 PDT 2010


Author: dgregor
Date: Tue Jul 27 09:52:07 2010
New Revision: 109505

URL: http://llvm.org/viewvc/llvm-project?rev=109505&view=rev
Log:
Fix use-after-free with precompiled preambles

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=109505&r1=109504&r2=109505&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Tue Jul 27 09:52:07 2010
@@ -135,6 +135,11 @@
   /// file within the precompiled preamble.
   unsigned PreambleReservedSize;
   
+  /// \brief When non-NULL, this is the buffer used to store the contents of
+  /// the main file when it has been padded for use with the precompiled
+  /// preamble.
+  llvm::MemoryBuffer *SavedMainFileBuffer;
+  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
   

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=109505&r1=109504&r2=109505&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Tue Jul 27 09:52:07 2010
@@ -39,7 +39,7 @@
 
 ASTUnit::ASTUnit(bool _MainFileIsAST)
   : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), 
-    ConcurrencyCheckValue(CheckUnlocked) { }
+    ConcurrencyCheckValue(CheckUnlocked), SavedMainFileBuffer(0) { }
 
 ASTUnit::~ASTUnit() {
   ConcurrencyCheckValue = CheckLocked;
@@ -60,6 +60,8 @@
          ++FB)
       delete FB->second;
   }
+  
+  delete SavedMainFileBuffer;
 }
 
 void ASTUnit::CleanTemporaryFiles() {
@@ -328,6 +330,9 @@
 /// \returns True if a failure occurred that causes the ASTUnit not to
 /// contain any translation-unit information, false otherwise.
 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
+  delete SavedMainFileBuffer;
+  SavedMainFileBuffer = 0;
+  
   if (!Invocation.get())
     return true;
   
@@ -395,6 +400,9 @@
                                                     = PreambleEndsAtStartOfLine;
     PreprocessorOpts.ImplicitPCHInclude = PreambleFile.str();
     PreprocessorOpts.DisablePCHValidation = true;
+    
+    // Keep track of the override buffer;
+    SavedMainFileBuffer = OverrideMainBuffer;
   }
   
   llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
@@ -787,7 +795,6 @@
   if (!AST->Parse(OverrideMainBuffer))
     return AST.take();
   
-  delete OverrideMainBuffer;
   return 0;
 }
 
@@ -884,6 +891,5 @@
 
   // Parse the sources
   bool Result = Parse(OverrideMainBuffer);  
-  delete OverrideMainBuffer;
   return Result;
 }





More information about the cfe-commits mailing list