[llvm-branch-commits] [cfe-branch] r127202 - in /cfe/branches/Apple/sill: lib/Serialization/ASTReaderStmt.cpp lib/Serialization/ASTWriterStmt.cpp test/PCH/cxx-templates.cpp

Douglas Gregor dgregor at apple.com
Mon Mar 7 17:50:45 PST 2011


Author: dgregor
Date: Mon Mar  7 19:50:45 2011
New Revision: 127202

URL: http://llvm.org/viewvc/llvm-project?rev=127202&view=rev
Log:
Merge r127125:

Author: andersca
Date: Sun Mar  6 12:19:42 2011
New Revision: 127125

URL: http://llvm.org/viewvc/llvm-project?rev=127125&view=rev
Log:
When serializing a DeclRefExpr, always store the number of explicit template
arguments at the same offset, since it's needed when creating the empty
DeclRefExpr when deserializing. Fixes a memory corruption issue that would lead
to random bugs and crashes.


Modified:
    cfe/branches/Apple/sill/lib/Serialization/ASTReaderStmt.cpp
    cfe/branches/Apple/sill/lib/Serialization/ASTWriterStmt.cpp
    cfe/branches/Apple/sill/test/PCH/cxx-templates.cpp

Modified: cfe/branches/Apple/sill/lib/Serialization/ASTReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/sill/lib/Serialization/ASTReaderStmt.cpp?rev=127202&r1=127201&r2=127202&view=diff
==============================================================================
--- cfe/branches/Apple/sill/lib/Serialization/ASTReaderStmt.cpp (original)
+++ cfe/branches/Apple/sill/lib/Serialization/ASTReaderStmt.cpp Mon Mar  7 19:50:45 2011
@@ -412,7 +412,10 @@
 
   bool HasQualifier = Record[Idx++];
   bool HasExplicitTemplateArgs = Record[Idx++];
-  
+  unsigned NumTemplateArgs = 0;
+  if (HasExplicitTemplateArgs)
+    NumTemplateArgs = Record[Idx++];
+
   E->DecoratedD.setInt((HasQualifier? DeclRefExpr::HasQualifierFlag : 0) |
       (HasExplicitTemplateArgs 
          ? DeclRefExpr::HasExplicitTemplateArgumentListFlag : 0));
@@ -422,11 +425,9 @@
     E->getNameQualifier()->Range = ReadSourceRange(Record, Idx);
   }
 
-  if (HasExplicitTemplateArgs) {
-    unsigned NumTemplateArgs = Record[Idx++];
+  if (HasExplicitTemplateArgs)
     ReadExplicitTemplateArgumentList(E->getExplicitTemplateArgs(),
                                      NumTemplateArgs);
-  }
 
   E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
   E->setLocation(ReadSourceLocation(Record, Idx));

Modified: cfe/branches/Apple/sill/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/sill/lib/Serialization/ASTWriterStmt.cpp?rev=127202&r1=127201&r2=127202&view=diff
==============================================================================
--- cfe/branches/Apple/sill/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/branches/Apple/sill/lib/Serialization/ASTWriterStmt.cpp Mon Mar  7 19:50:45 2011
@@ -373,16 +373,18 @@
   Record.push_back(E->hasQualifier());
   Record.push_back(E->hasExplicitTemplateArgs());
 
+  if (E->hasExplicitTemplateArgs()) {
+    unsigned NumTemplateArgs = E->getNumTemplateArgs();
+    Record.push_back(NumTemplateArgs);
+  }
+
   if (E->hasQualifier()) {
     Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
     Writer.AddSourceRange(E->getQualifierRange(), Record);
   }
 
-  if (E->hasExplicitTemplateArgs()) {
-    unsigned NumTemplateArgs = E->getNumTemplateArgs();
-    Record.push_back(NumTemplateArgs);
+  if (E->hasExplicitTemplateArgs())
     AddExplicitTemplateArgumentList(E->getExplicitTemplateArgs());
-  }
 
   Writer.AddDeclRef(E->getDecl(), Record);
   Writer.AddSourceLocation(E->getLocation(), Record);

Modified: cfe/branches/Apple/sill/test/PCH/cxx-templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/sill/test/PCH/cxx-templates.cpp?rev=127202&r1=127201&r2=127202&view=diff
==============================================================================
--- cfe/branches/Apple/sill/test/PCH/cxx-templates.cpp (original)
+++ cfe/branches/Apple/sill/test/PCH/cxx-templates.cpp Mon Mar  7 19:50:45 2011
@@ -43,3 +43,22 @@
 namespace ZeroLengthExplicitTemplateArgs {
   template void f<X>(X*);
 }
+
+// This used to overwrite memory and crash.
+namespace Test1 {
+  struct StringHasher {
+    template<typename T, char Converter(T)> static inline unsigned createHash(const T*, unsigned) {
+      return 0;
+    }
+  };
+
+  struct CaseFoldingHash {
+    static inline char foldCase(char) {
+      return 0;
+    }
+
+    static unsigned hash(const char* data, unsigned length) {
+      return StringHasher::createHash<char, foldCase>(data, length);
+    }
+  };
+}





More information about the llvm-branch-commits mailing list