]> granicus.if.org Git - clang/commitdiff
Fix a really subtle bug in the macro expander caching code, where
authorChris Lattner <sabre@nondot.org>
Sun, 22 Jul 2007 01:16:55 +0000 (01:16 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 22 Jul 2007 01:16:55 +0000 (01:16 +0000)
redefinition of a macro could cause invalid memory to be deleted.
Found preprocessing 253.perlbmk.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40380 91177308-0d34-0410-b5e6-96231b3b80d8

Lex/MacroExpander.cpp
clang.xcodeproj/project.pbxproj
include/clang/Lex/MacroExpander.h

index fade8546572f198e20840cab584b1023765bbf1b..57bdfccc1103e89602678fc297397f0153816613 100644 (file)
@@ -245,6 +245,7 @@ void MacroExpander::Init(Token &Tok, MacroArgs *Actuals) {
   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 @@ void MacroExpander::Init(const Token *TokArray, unsigned NumToks) {
   Macro = 0;
   ActualArgs = 0;
   MacroTokens = TokArray;
+  OwnsMacroTokens = false;
   NumMacroTokens = NumToks;
   CurToken = 0;
   InstantiateLoc = SourceLocation();
@@ -288,8 +290,10 @@ void MacroExpander::Init(const Token *TokArray, unsigned NumToks) {
 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 @@ void MacroExpander::ExpandFunctionArguments() {
     if (NumMacroTokens)
       memcpy(Res, &ResultToks[0], NumMacroTokens*sizeof(Token));
     MacroTokens = Res;
+    OwnsMacroTokens = true;
   }
 }
 
index ff84ac7c23151c701bc56bf555be41593b7f68f5..bb772fa1827bfe2c9e25f8e9a3c44b87b16d0846 100644 (file)
                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>"; };
index acc9fc6238db12dba0216ed2f76b4cca412a2685..b0027bbb96e2ab186276a6a370cfb9f1028fc1ce 100644 (file)
@@ -134,7 +134,12 @@ class MacroExpander {
   
   /// 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 @@ public:
   /// 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 @@ public:
   /// 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);
   }