This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix c++/22153


In the following invalid code:

    template<int> void foo();

    template<int> struct A
    {
      template<> friend void foo<0>();
    };

We are parsing the invalid member function declaration as if it were a
member template declaration.  So, it's being added to the
unparsed_functions_queue for post-processing.  However, the functions
that perform this post-processing (starting with
cp_parser_late_parsing_for_member) make the assumption that if
DECL_TEMPLATE_INFO is set then this function will have valid template
info in DECL_TI_TEMPLATE.  In this case, however, even though
DECL_TEMPLATE_INFO is set, DECL_TI_TEMPLATE represents the class
template for this friend declaration and not the function template info.
 The function inline_needs_template_parms fails to handle this and
dereferences a NULL pointer returned by most_general_template.  Fixed by
checking the value before dereferencing it.

I also considered instead not adding the invalid declaration to the
unparsed_functions_queue, but thought that since we were attempting to
parse the function as an invalid template function (instead of as an
invalid specialization), allowing it to be post-processed would be most
consistent (and provide some, possibly useful?, additional checking).

Note that the PR indicated this as a 3.4, 4.0, and 4.1 regression,
however I only see a segfault on 4.1.  On 3.4 and 4.0 I see the expected
warnings followed by:

  bad.c:6: confused by earlier errors, bailing out

Should I look for a patch for this, too, or is it acceptable?

Patch tested on arm-none-elf and i686-pc-linux-gnu with no regressions.
 OK for mainline?

- Josh

2005-09-20  Josh Conner  <jconner@apple.com>

	PR c++/22153
	* pt.c (inline_needs_template_parms): Check result of
	call to most_general_template before using it.

2005-09-20  Josh Conner  <jconner@apple.com>

	PR c++/22153
	* g++.dg/template/crash38.C: New test.


Index: gcc/cp/pt.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/cp/pt.c,v
retrieving revision 1.1038
diff -c -3 -p -r1.1038 pt.c
*** gcc/cp/pt.c	16 Sep 2005 15:41:38 -0000	1.1038
--- gcc/cp/pt.c	20 Sep 2005 01:12:34 -0000
*************** template_class_depth (tree type)
*** 322,331 ****
  static int
  inline_needs_template_parms (tree decl)
  {
    if (! DECL_TEMPLATE_INFO (decl))
      return 0;
  
!   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
  	  > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
  }
  
--- 322,338 ----
  static int
  inline_needs_template_parms (tree decl)
  {
+   tree tmpl;
+ 
    if (! DECL_TEMPLATE_INFO (decl))
      return 0;
  
!   tmpl = most_general_template (decl);
! 
!   if (tmpl == NULL_TREE)
!     return 0;
! 
!   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
  	  > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
  }
  

// { dg-do compile }

// PR c++/22153

template<int> void foo();

template<int> struct A
{
    template<> friend void foo<0>(); // { dg-error "" }
};


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]