[cfe-commits] r40380 - in /cfe/trunk: Lex/MacroExpander.cpp clang.xcodeproj/project.pbxproj include/clang/Lex/MacroExpander.h

Chris Lattner sabre at nondot.org
Sat Jul 21 18:16:56 PDT 2007


Author: lattner
Date: Sat Jul 21 20:16:55 2007
New Revision: 40380

URL: http://llvm.org/viewvc/llvm-project?rev=40380&view=rev
Log:
Fix a really subtle bug in the macro expander caching code, where
redefinition of a macro could cause invalid memory to be deleted.
Found preprocessing 253.perlbmk.

Modified:
    cfe/trunk/Lex/MacroExpander.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/Lex/MacroExpander.h

Modified: cfe/trunk/Lex/MacroExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/MacroExpander.cpp?rev=40380&r1=40379&r2=40380&view=diff

==============================================================================
--- cfe/trunk/Lex/MacroExpander.cpp (original)
+++ cfe/trunk/Lex/MacroExpander.cpp Sat Jul 21 20:16:55 2007
@@ -245,6 +245,7 @@
   AtStartOfLine = Tok.isAtStartOfLine();
   HasLeadingSpace = Tok.hasLeadingSpace();
   MacroTokens = &*Macro->tokens_begin();
+  OwnsMacroTokens = false;
   NumMacroTokens = Macro->tokens_end()-Macro->tokens_begin();
 
   // If this is a function-like macro, expand the arguments and change
@@ -270,6 +271,7 @@
   Macro = 0;
   ActualArgs = 0;
   MacroTokens = TokArray;
+  OwnsMacroTokens = false;
   NumMacroTokens = NumToks;
   CurToken = 0;
   InstantiateLoc = SourceLocation();
@@ -288,8 +290,10 @@
 void MacroExpander::destroy() {
   // If this was a function-like macro that actually uses its arguments, delete
   // the expanded tokens.
-  if (Macro && MacroTokens != &*Macro->tokens_begin())
+  if (OwnsMacroTokens) {
     delete [] MacroTokens;
+    MacroTokens = 0;
+  }
   
   // MacroExpander owns its formal arguments.
   if (ActualArgs) ActualArgs->destroy();
@@ -455,6 +459,7 @@
     if (NumMacroTokens)
       memcpy(Res, &ResultToks[0], NumMacroTokens*sizeof(Token));
     MacroTokens = Res;
+    OwnsMacroTokens = true;
   }
 }
 

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=40380&r1=40379&r2=40380&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Sat Jul 21 20:16:55 2007
@@ -191,7 +191,7 @@
 		1A869AA70BA21ABA008DA07A /* LiteralSupport.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = LiteralSupport.cpp; sourceTree = "<group>"; };
 		84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
 		84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
 		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };

Modified: cfe/trunk/include/clang/Lex/MacroExpander.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/MacroExpander.h?rev=40380&r1=40379&r2=40380&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/MacroExpander.h (original)
+++ cfe/trunk/include/clang/Lex/MacroExpander.h Sat Jul 21 20:16:55 2007
@@ -134,7 +134,12 @@
   
   /// Lexical information about the expansion point of the macro: the identifier
   /// that the macro expanded from had these properties.
-  bool AtStartOfLine, HasLeadingSpace;
+  bool AtStartOfLine : 1;
+  bool HasLeadingSpace : 1;
+  
+  /// OwnsMacroTokens - This is true if this macroexpander allocated the
+  /// MacroTokens array, and thus needs to free it when destroyed.
+  bool OwnsMacroTokens : 1;
   
   MacroExpander(const MacroExpander&);  // DO NOT IMPLEMENT
   void operator=(const MacroExpander&); // DO NOT IMPLEMENT
@@ -142,7 +147,7 @@
   /// Create a macro expander for the specified macro with the specified actual
   /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
   MacroExpander(Token &Tok, MacroArgs *ActualArgs, Preprocessor &pp)
-    : Macro(0), ActualArgs(0), PP(pp) {
+    : Macro(0), ActualArgs(0), PP(pp), OwnsMacroTokens(false) {
     Init(Tok, ActualArgs);
   }
   
@@ -154,7 +159,7 @@
   /// Create a macro expander for the specified token stream.  This does not
   /// take ownership of the specified token vector.
   MacroExpander(const Token *TokArray, unsigned NumToks, Preprocessor &pp)
-    : Macro(0), ActualArgs(0), PP(pp) {
+    : Macro(0), ActualArgs(0), PP(pp), OwnsMacroTokens(false) {
     Init(TokArray, NumToks);
   }
   





More information about the cfe-commits mailing list