This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix PR c++/28420: ICE with "typeid" as template parameter
- From: Simon Martin <simartin at users dot sourceforge dot net>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 19 Aug 2006 09:31:10 +0200
- Subject: [PATCH] Fix PR c++/28420: ICE with "typeid" as template parameter
Hello.
The following invalid code snippet triggers an ICE with the mainline:
=== cut here ===
template<int> struct A;
int i = sizeof(A<typeid>);
=== cut here ===
=== error ===
[simon@texel pr28420]$ g++-head -c pr28420.cpp
pr28420.cpp:3: error: `typeid' operator cannot appear in a constant-expression
pr28420.cpp:3: error: template argument 1 is invalid
*** glibc detected *** double free or corruption (out): 0x0866ddc0 ***
pr28420.cpp:3: internal compiler error: Aborted
=== error ===
This is due to cp_parser_postfix_expression setting
parser->type_definition_forbidden_message to a stack allocated string ("types
may not be defined in a `typeid\' expression"), and not restoring the initial
value before returning in the present error case, hence the dangling pointer.
This patch fixes this by ensuring that the initial value for
parser->type_definition_forbidden_message is always restored before
returning from cp_parser_postfix_expression.
Bootstrapped and regtested with no new unexpected failures on
i686-pc-linux-gnu. Is it OK? If so, could someone commit it for me please?
Best regards,
Simon
:ADDPATCH c++:
2006-08-18 Simon Martin <simartin@users.sourceforge.net>
PR c++/28420
* parser.c (cp_parser_postfix_expression): Make sure that the saved value
for parser->type_definition_forbidden_message is restored before returning
to avoid an invalid free().
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c (revision 116251)
+++ gcc/cp/parser.c (working copy)
@@ -4078,12 +4078,12 @@ cp_parser_postfix_expression (cp_parser
/* Look for the `)' token. */
cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
}
+ /* Restore the saved message. */
+ parser->type_definition_forbidden_message = saved_message;
/* `typeid' may not appear in an integral constant expression. */
if (cp_parser_non_integral_constant_expression(parser,
"`typeid' operator"))
return error_mark_node;
- /* Restore the saved message. */
- parser->type_definition_forbidden_message = saved_message;
}
break;
2006-08-18 Simon Martin <simartin@users.sourceforge.net>
PR c++/28420
* g++.dg/template/typeid-template-argument.C: New test.
/* This used to ICE (PR28420) */
/* { dg-do compile } */
template<int> struct A;
int i = sizeof(A<typeid>); /* { dg-error "operator cannot appear in a constant-expression|template argument 1 is invalid" } */