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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40380 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Lex/MacroExpander.cpp b/Lex/MacroExpander.cpp
index fade854..57bdfcc 100644
--- a/Lex/MacroExpander.cpp
+++ b/Lex/MacroExpander.cpp
@@ -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;
}
}