Bug 77804 - Internal compiler error on incorrect initialization of new-d array
Summary: Internal compiler error on incorrect initialization of new-d array
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2016-09-30 14:52 UTC by Vlad Gheorghiu
Modified: 2016-10-04 17:57 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 6.2.0, 7.0
Last reconfirmed: 2016-09-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vlad Gheorghiu 2016-09-30 14:52:29 UTC
The code below

#include <new>

int main()
{
    char buf[256];
    std::size_t n = 10;
    int* p = new (buf) (int[n]);  // incorrect way, parenthesis by mistake
    // int* p = new (buf) int[n]; // correct way
}

produces an internal compile error in gcc versions 6 or later. It compiles fine (although with an warning: non-constant array new length must be specified without parentheses around the type-id [-Wvla].
Comment 1 Martin Sebor 2016-09-30 18:09:14 UTC
Confirmed.

$ cat v.C && /build/gcc-trunk-git/gcc/xgcc -B /build/gcc-trunk-git/gcc -S v.Cvoid* operator new[] (__SIZE_TYPE__ n, void *p) { return p; }

int main()
{
    char buf[256];
    unsigned n = 10;
    int* p = new (buf) (int[n]);  // incorrect way, parenthesis by mistake
}
v.C: In function ‘int main()’:
v.C:7:31: warning: non-constant array new length must be specified without parentheses around the type-id [-Wvla]
     int* p = new (buf) (int[n]);  // incorrect way, parenthesis by mistake
                               ^
v.C:7:31: internal compiler error: in tree_to_uhwi, at tree.c:7330
0x1464ef6 tree_to_uhwi(tree_node const*)
	/src/gcc/git/gcc/tree.c:7330
0x9b9d9c warn_placement_new_too_small
	/src/gcc/git/gcc/cp/init.c:2530
0x9bc037 build_new_1
	/src/gcc/git/gcc/cp/init.c:3060
0x9bd294 build_new(vec<tree_node*, va_gc, vl_embed>**, tree_node*, tree_node*, vec<tree_node*, va_gc, vl_embed>**, int, int)
	/src/gcc/git/gcc/cp/init.c:3515
0x928c05 cp_parser_new_expression
	/src/gcc/git/gcc/cp/parser.c:8171
0x927dc8 cp_parser_unary_expression
	/src/gcc/git/gcc/cp/parser.c:7771
0x9295fa cp_parser_cast_expression
	/src/gcc/git/gcc/cp/parser.c:8696
0x9296e8 cp_parser_binary_expression
	/src/gcc/git/gcc/cp/parser.c:8798
0x92a405 cp_parser_assignment_expression
	/src/gcc/git/gcc/cp/parser.c:9086
0x92ab5d cp_parser_constant_expression
	/src/gcc/git/gcc/cp/parser.c:9354
0x940240 cp_parser_initializer_clause
	/src/gcc/git/gcc/cp/parser.c:21048
0x940089 cp_parser_initializer
	/src/gcc/git/gcc/cp/parser.c:20986
0x93bf31 cp_parser_init_declarator
	/src/gcc/git/gcc/cp/parser.c:18851
0x93139e cp_parser_simple_declaration
	/src/gcc/git/gcc/cp/parser.c:12567
0x93115f cp_parser_block_declaration
	/src/gcc/git/gcc/cp/parser.c:12435
0x9305d7 cp_parser_declaration_statement
	/src/gcc/git/gcc/cp/parser.c:12047
0x92d3b4 cp_parser_statement
	/src/gcc/git/gcc/cp/parser.c:10599
0x92dfc5 cp_parser_statement_seq_opt
	/src/gcc/git/gcc/cp/parser.c:10931
0x92dec0 cp_parser_compound_statement
	/src/gcc/git/gcc/cp/parser.c:10885
0x93fe1f cp_parser_function_body
	/src/gcc/git/gcc/cp/parser.c:20905
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.
Comment 2 Martin Sebor 2016-10-03 20:54:44 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2016-10/msg00112.html
Comment 3 Martin Sebor 2016-10-04 17:34:32 UTC
Author: msebor
Date: Tue Oct  4 17:34:00 2016
New Revision: 240754

URL: https://gcc.gnu.org/viewcvs?rev=240754&root=gcc&view=rev
Log:
PR c++/77804 - Internal compiler error on incorrect initialization of new-d array

gcc/cp/ChangeLog:

	PR c++/77804
	* init.c (warn_placement_new_too_small): Avoid assuming an array type
	has a constant size.

gcc/testsuite/ChangeLog:

	PR c++/77804
	* g++.dg/warn/Wplacement-new-size-4.C: New test.


Added:
    trunk/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/init.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Martin Sebor 2016-10-04 17:56:15 UTC
Author: msebor
Date: Tue Oct  4 17:55:43 2016
New Revision: 240755

URL: https://gcc.gnu.org/viewcvs?rev=240755&root=gcc&view=rev
Log:
PR c++/77804 - Internal compiler error on incorrect initialization of new-d array

gcc/cp/ChangeLog:

	PR c++/77804
	* init.c (warn_placement_new_too_small): Avoid assuming an array type
	has a constant size.

gcc/testsuite/ChangeLog:

	PR c++/77804
	* g++.dg/warn/Wplacement-new-size-4.C: New test.


Added:
    branches/gcc-6-branch/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C
Modified:
    branches/gcc-6-branch/gcc/cp/ChangeLog
    branches/gcc-6-branch/gcc/cp/init.c
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
Comment 5 Martin Sebor 2016-10-04 17:57:27 UTC
Fixed for 7.0 in r240754 and for 6.3 in r240755.