[PATCH] Prevent crash on template function specialization in class scope

Richard Trieu rtrieu at google.com
Wed May 15 15:54:21 PDT 2013


class Foo {
  template<class T>
  static void Bar(const T& input);

  template<>
  static void Bar(const long& input) {}
};

This code causing Clang to crash when Bar is instantiated with the long type.  This patch adds a check for a null pointer to prevent dereferencing it.

http://llvm-reviews.chandlerc.com/D799

Files:
  test/SemaTemplate/function-template-specialization.cpp
  lib/Sema/SemaDecl.cpp

Index: test/SemaTemplate/function-template-specialization.cpp
===================================================================
--- test/SemaTemplate/function-template-specialization.cpp
+++ test/SemaTemplate/function-template-specialization.cpp
@@ -46,3 +46,12 @@
   template <typename T> void f(T t) {}
   template <typename T> void f<T*>(T* t) {} // expected-error{{function template partial specialization is not allowed}}
 }
+
+class Foo {
+  template<class T>
+  static void Bar(const T& input);
+
+  // Don't crash here.
+  template<>
+  static void Bar(const long& input) {}  // expected-error{{explicit specialization of 'Bar' in class scope}}
+};
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6405,8 +6405,10 @@
       // C++ [dcl.stc]p1:
       //   A storage-class-specifier shall not be specified in an explicit
       //   specialization (14.7.3)
-      if (SC != SC_None) {
-        if (SC != NewFD->getTemplateSpecializationInfo()->getTemplate()->getTemplatedDecl()->getStorageClass())
+      FunctionTemplateSpecializationInfo *Info =
+          NewFD->getTemplateSpecializationInfo();
+      if (Info && SC != SC_None) {
+        if (SC != Info->getTemplate()->getTemplatedDecl()->getStorageClass())
           Diag(NewFD->getLocation(),
                diag::err_explicit_specialization_inconsistent_storage_class)
             << SC
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D799.1.patch
Type: text/x-patch
Size: 1462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130515/eba6f731/attachment.bin>


More information about the cfe-commits mailing list