Bug 29435 - [4.1 Regression] seg fault with sizeof and templates
Summary: [4.1 Regression] seg fault with sizeof and templates
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P1 blocker
Target Milestone: 4.1.2
Assignee: Mark Mitchell
URL:
Keywords: ice-on-invalid-code, ice-on-valid-code
: 29698 30042 (view as bug list)
Depends on: 29226
Blocks:
  Show dependency treegraph
 
Reported: 2006-10-11 23:00 UTC by Martin Michlmayr
Modified: 2006-12-02 07:35 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.0.4
Known to fail: 4.1.2 4.2.0
Last reconfirmed: 2006-10-12 03:36:29


Attachments
testcase (533 bytes, text/plain)
2006-10-11 23:01 UTC, Martin Michlmayr
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Michlmayr 2006-10-11 23:00:24 UTC
I get a segmentation fault compiling the attached testcase with gcc 4.1.  4.0 and 4.2 seem to work.   4.1.2 20060901 (Debian 4.1.1-13) also worked but
4.1.2 20061007 (Debian 4.1.1-16) fails, at least on x86_64.



(sid)1142:tbm@em64t: ~] g++-4.1 -c cinepaint-plugin_main.cpp
cinepaint-plugin_main.cpp: In constructor ‘Camera<Unsign, Real>::Camera(Br_ImageVec&, std::vector<TNT::Array2D<Rgb<Unsign> >, std::allocator<TNT::Array2D<Rgb<Unsign> > > >&, unsigned int, ProgressInfo*)’:
cinepaint-plugin_main.cpp:76: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 1 Martin Michlmayr 2006-10-11 23:01:02 UTC
Created attachment 12413 [details]
testcase
Comment 2 Martin Michlmayr 2006-10-11 23:02:53 UTC
Ignore my comment about x86_64.  I also see this on mips.
Comment 3 Andrew Pinski 2006-10-11 23:05:52 UTC
I think this is a sizeof issue:
sizeof (Rgb < XYindex >)
Comment 4 Martin Michlmayr 2006-10-11 23:19:28 UTC
(gdb) where
#0  0x000000000045a5ce in cxx_sizeof_or_alignof_type (type=0x2b29cf3e6b00, op=214, complain=1 '\001')
    at /home/tbm/scratch/gcc-4.1/gcc/cp/typeck.c:1251
#1  0x000000000044a947 in cp_parser_unary_expression (parser=0x2b29cf261460, address_p=0 '\0',
    cast_p=0 '\0') at /home/tbm/scratch/gcc-4.1/gcc/cp/parser.c:4850
#2  0x000000000044ae15 in cp_parser_assignment_expression (parser=0x2b29cf261460, cast_p=Variable "cast_p" is not available.
)
    at /home/tbm/scratch/gcc-4.1/gcc/cp/parser.c:5579
#3  0x000000000044ba72 in cp_parser_parenthesized_expression_list (parser=0x2b29cf261460,
    is_attribute_list=240 '▒', cast_p=0 '\0', non_constant_p=0x0)
    at /home/tbm/scratch/gcc-4.1/gcc/cp/parser.c:4659
#4  0x000000000044a324 in cp_parser_unary_expression (parser=0x2b29cf261460, address_p=0 '\0',
    cast_p=1 '\001') at /home/tbm/scratch/gcc-4.1/gcc/cp/parser.c:4204
#5  0x000000000044ac41 in cp_parser_cast_expression (parser=0x2b29cf261460, address_p=0 '\0',
    cast_p=0 '\0') at /home/tbm/scratch/gcc-4.1/gcc/cp/parser.c:5455
...
Comment 5 Andrew Pinski 2006-10-12 03:36:29 UTC
Reduced testcase:
template < class T > struct Rgb;
template < int>int Camera1 ()
{
sizeof (Rgb < int>);
}
Comment 6 Andrew Pinski 2006-10-12 03:37:24 UTC
The code in comment # 5 is invalid but even if we make it valid like below, it still ICEs:
template < class T > struct Rgb{};
template < int>int Camera1 ()
{
sizeof (Rgb < int>);
}
Comment 7 Andrew Pinski 2006-10-12 03:39:40 UTC
Works in 4.0.4 20061011.
Comment 8 Andrew Pinski 2006-10-12 03:44:07 UTC
I think this was caused by PR 29226.
Comment 9 Andrew Pinski 2006-10-12 03:48:08 UTC
Also ICEs with 4.2.0 20061012 but did not with 4.2.0 20061002 (which I think was built before PR 29226 came in).
Comment 10 Andrew Pinski 2006-10-12 03:57:57 UTC
The problem is that we have not layouted out the type yet, if we had with something like:
template < class T >
struct Rgb{};
Rgb<int> t;
template < int>int Camera1 ()
{
sizeof (Rgb < int>);
}

We don't crash.
Something like this fixes the ICE but I don't know if we should not call dependent_type_p twice or not, I have not checked if does causes an ICE for the testcase of PR 29226 or not:
Index: ../../gcc/cp/typeck.c
===================================================================
--- ../../gcc/cp/typeck.c       (revision 117656)
+++ ../../gcc/cp/typeck.c       (working copy)
@@ -1256,6 +1256,9 @@ cxx_sizeof_or_alignof_type (tree type, e
       value = size_one_node;
     }

+  if (!dependent_type_p (type))
+    type = complete_type (type);
+
   if (dependent_type_p (type)
       /* VLA types will have a non-constant size.  In the body of an
         uninstantiated template, we don't need to try to compute the
@@ -1271,7 +1274,7 @@ cxx_sizeof_or_alignof_type (tree type, e
       return value;
     }

-  return c_sizeof_or_alignof_type (complete_type (type),
+  return c_sizeof_or_alignof_type (type,
                                   op == SIZEOF_EXPR,
                                   complain);
 }
Comment 11 Mark Mitchell 2006-10-16 23:06:48 UTC
Subject: Bug 29435

Author: mmitchel
Date: Mon Oct 16 23:06:35 2006
New Revision: 117799

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=117799
Log:
	PR c++/29435
	* typeck.c (cxx_sizeof_or_alignof_type): Complete non-dependent
	types when their sizes are required.  Refine test for VLAs.
	PR c++/29435
	* g++.dg/template/sizeof11.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/template/sizeof11.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog

Comment 12 Mark Mitchell 2006-10-16 23:11:39 UTC
Fixed in 4.2.0.
Comment 13 Mark Mitchell 2006-10-17 01:56:33 UTC
Subject: Bug 29435

Author: mmitchel
Date: Tue Oct 17 01:56:23 2006
New Revision: 117813

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=117813
Log:
	PR c++/29435
	* typeck.c (cxx_sizeof_or_alignof_type): Complete non-dependent
	types when their sizes are required.  Refine test for VLAs.
	PR c++/29435
	* g++.dg/template/sizeof11.C: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/template/sizeof11.C
Modified:
    branches/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/gcc-4_1-branch/gcc/cp/typeck.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 14 Mark Mitchell 2006-10-17 01:56:42 UTC
Fixed in 4.1.2.
Comment 15 Andrew Pinski 2006-11-03 18:29:53 UTC
*** Bug 29698 has been marked as a duplicate of this bug. ***
Comment 16 Andrew Pinski 2006-12-02 07:35:24 UTC
*** Bug 30042 has been marked as a duplicate of this bug. ***