gcc/include
Pedro Alves 921da19854 PR other/61321 - demangler crash on casts in template parameters
The fix for bug 59195:

 [C++ demangler handles conversion operator incorrectly]
 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59195

unfortunately makes the demangler crash due to infinite recursion, in
case of casts in template parameters.

For example, with:

 template<int> struct A {};
 template <typename Y> void function_temp(A<sizeof ((Y)(999))>) {}
 template void function_temp<int>(A<sizeof (int)>);

The 'function_temp<int>' instantiation above mangles to:

  _Z13function_tempIiEv1AIXszcvT_Li999EEE

The demangler parses this as:

typed name
  template
    name 'function_temp'
    template argument list
      builtin type int
  function type
    builtin type void
    argument list
      template                          (*)
        name 'A'
        template argument list
          unary operator
            operator sizeof
            unary operator
              cast
                template parameter 0    (**)
              literal
                builtin type int
                name '999'

And after the fix for 59195, due to:

 static void
 d_print_cast (struct d_print_info *dpi, int options,
	       const struct demangle_component *dc)
 {
 ...
   /* For a cast operator, we need the template parameters from
      the enclosing template in scope for processing the type.  */
   if (dpi->current_template != NULL)
     {
       dpt.next = dpi->templates;
       dpi->templates = &dpt;
       dpt.template_decl = dpi->current_template;
     }

when printing the template argument list of A (what should be "<sizeof
(int)>"), the template parameter 0 (that is, "T_", the '**' above) now
refers to the first parameter of the the template argument list of the
'A' template (the '*' above), exactly what we were already trying to
print.  This leads to infinite recursion, and stack exaustion.  The
template parameter 0 should actually refer to the first parameter of
the 'function_temp' template.

Where it reads "for the cast operator" in the comment in d_print_cast
(above), it's really talking about a conversion operator, like:

  struct A { template <typename U> explicit operator U(); };

We don't want to inject the template parameters from the enclosing
template in scope when processing a cast _expression_, only when
handling a conversion operator.

The problem is that DEMANGLE_COMPONENT_CAST is currently ambiguous,
and means _both_ 'conversion operator' and 'cast expression'.

Fix this by adding a new DEMANGLE_COMPONENT_CONVERSION component type,
which does what DEMANGLE_COMPONENT_CAST does today, and making
DEMANGLE_COMPONENT_CAST just simply print its component subtree.

I think we could instead reuse DEMANGLE_COMPONENT_CAST and in
d_print_comp_inner still do:

 @@ -5001,9 +5013,9 @@ d_print_comp_inner (struct d_print_info *dpi, int options,
        d_print_comp (dpi, options, dc->u.s_extended_operator.name);
        return;

     case DEMANGLE_COMPONENT_CAST:
       d_append_string (dpi, "operator ");
 -     d_print_cast (dpi, options, dc);
 +     d_print_conversion (dpi, options, dc);
       return;

leaving the unary cast case below calling d_print_cast, but seems to
me that spliting the component types makes it easier to reason about
the code.

g++'s testsuite actually generates three symbols that crash the
demangler in the same way.  I've added those as tests in the demangler
testsuite as well.

And then this fixes PR other/61233 too, which happens to be a
demangler crash originally reported to GDB, at:
https://sourceware.org/bugzilla/show_bug.cgi?id=16957

Bootstrapped and regtested on x86_64 Fedora 20.

Also ran this through GDB's testsuite.  GDB will require a small
update to use DEMANGLE_COMPONENT_CONVERSION in one place it's using
DEMANGLE_COMPONENT_CAST in its sources.

libiberty/
2015-11-27  Pedro Alves  <palves@redhat.com>

        PR other/61321
        PR other/61233
        * demangle.h (enum demangle_component_type)
        <DEMANGLE_COMPONENT_CONVERSION>: New value.
        * cp-demangle.c (d_demangle_callback, d_make_comp): Handle
        DEMANGLE_COMPONENT_CONVERSION.
        (is_ctor_dtor_or_conversion): Handle DEMANGLE_COMPONENT_CONVERSION
        instead of DEMANGLE_COMPONENT_CAST.
        (d_operator_name): Return a DEMANGLE_COMPONENT_CONVERSION
        component if handling a conversion.
        (d_count_templates_scopes, d_print_comp_inner): Handle
        DEMANGLE_COMPONENT_CONVERSION.
        (d_print_comp_inner): Handle DEMANGLE_COMPONENT_CONVERSION instead
        of DEMANGLE_COMPONENT_CAST.
        (d_print_cast): Rename as ...
        (d_print_conversion): ... this.  Adjust comments.
        (d_print_cast): Rewrite - simply print the left subcomponent.
        * cp-demint.c (cplus_demangle_fill_component): Handle
        DEMANGLE_COMPONENT_CONVERSION.

        * testsuite/demangle-expected: Add tests.

From-SVN: r231020
2015-11-27 14:48:21 +00:00
..
gdb Update copyright year in include 2015-07-14 09:08:59 -07:00
ansidecl.h add GCC_FINAL to ansidecl.h 2015-08-11 22:39:31 +00:00
ChangeLog Port libvtv to Solaris 2015-11-25 10:30:25 +00:00
ChangeLog-9103
ChangeLog.jit Merger of dmalcolm/jit branch from git 2014-11-11 21:55:52 +00:00
COPYING
COPYING3
demangle.h PR other/61321 - demangler crash on casts in template parameters 2015-11-27 14:48:21 +00:00
dwarf2.def DWARF2: add extensions to handle biased types 2015-08-14 09:48:13 +00:00
dwarf2.h Update copyright year in include 2015-07-14 09:08:59 -07:00
dyn-string.h Update copyright year in include 2015-07-14 09:08:59 -07:00
environ.h Fix PR63758 by using the _NSGetEnviron() API on Darwin 2015-10-18 10:33:37 +00:00
fibheap.h Update copyright year in include 2015-07-14 09:08:59 -07:00
filenames.h Update copyright year in include 2015-07-14 09:08:59 -07:00
floatformat.h Update copyright year in include 2015-07-14 09:08:59 -07:00
fnmatch.h Update copyright year in include 2015-07-14 09:08:59 -07:00
gcc-c-fe.def Update copyright year in include 2015-07-14 09:08:59 -07:00
gcc-c-interface.h Update copyright year in include 2015-07-14 09:08:59 -07:00
gcc-interface.h Update copyright year in include 2015-07-14 09:08:59 -07:00
getopt.h Update copyright year in include 2015-07-14 09:08:59 -07:00
gomp-constants.h c-pragma.c (oacc_pragmas): Add entry for declare directive. 2015-11-12 22:20:41 +00:00
hashtab.h Update copyright year in include 2015-07-14 09:08:59 -07:00
leb128.h Update copyright year in include 2015-07-14 09:08:59 -07:00
libiberty.h libiberty.h (asprintf): Don't declare if HAVE_DECL_ASPRINTF is not defined. 2015-05-22 20:53:45 +00:00
longlong.h config.gcc: Handle --enable-fdpic. 2015-10-27 13:36:47 +00:00
lto-symtab.h Update copyright year in include 2015-07-14 09:08:59 -07:00
md5.h Update copyright year in include 2015-07-14 09:08:59 -07:00
objalloc.h Update copyright year in include 2015-07-14 09:08:59 -07:00
obstack.h Modify obstack.[hc] to avoid having to include other gnulib files 2015-11-09 14:58:21 +10:30
partition.h partition.h (struct partition_elem): Re-order elements to avoid padding. 2015-03-19 11:44:08 +00:00
plugin-api.h Update copyright year in include 2015-07-14 09:08:59 -07:00
safe-ctype.h Update copyright year in include 2015-07-14 09:08:59 -07:00
sha1.h Update copyright year in include 2015-07-14 09:08:59 -07:00
simple-object.h Update copyright year in include 2015-07-14 09:08:59 -07:00
sort.h Update copyright year in include 2015-07-14 09:08:59 -07:00
splay-tree.h Update copyright year in include 2015-07-14 09:08:59 -07:00
symcat.h Update copyright year in include 2015-07-14 09:08:59 -07:00
timeval-utils.h Update copyright year in include 2015-07-14 09:08:59 -07:00
vtv-change-permission.h Port libvtv to Solaris 2015-11-25 10:30:25 +00:00
xregex2.h Update copyright year in include 2015-07-14 09:08:59 -07:00
xregex.h system.h [...]: Wrap C function declarations in extern "C". 2011-07-25 14:24:59 +00:00
xtensa-config.h Update copyright year in include 2015-07-14 09:08:59 -07:00