The attached patch removes an assumption from the implementation of the -Wplacement-new warning that the size of the array type enclosed in parentheses and accepted by G++ as an extension is constant. The assumption causes an ICE in 6.2.0 and 7.0.
Is the patch good to commit to both 7.0 and the 6 branch? Thanks Martin
PR c++/77804 - Internal compiler error on incorrect initialization of new-d array gcc/cp/ChangeLog: 2016-10-03 Martin Sebor <mse...@redhat.com> PR c++/77804 * init.c (warn_placement_new_too_small): Avoid assuming an array type has a constant size. gcc/testsuite/ChangeLog: 2016-10-03 Martin Sebor <mse...@redhat.com> PR c++/77804 * g++.dg/warn/Wplacement-new-size-4.C: New test. diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 798de08..30957f1 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -2504,7 +2504,7 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) && warn_placement_new < 2) return; } - + /* The size of the buffer can only be adjusted down but not up. */ gcc_checking_assert (0 <= adjust); @@ -2526,8 +2526,13 @@ warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) else if (nelts && CONSTANT_CLASS_P (nelts)) bytes_need = tree_to_uhwi (nelts) * tree_to_uhwi (TYPE_SIZE_UNIT (type)); - else + else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) bytes_need = tree_to_uhwi (TYPE_SIZE_UNIT (type)); + else + { + /* The type is a VLA. */ + return; + } if (bytes_avail < bytes_need) { diff --git a/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C new file mode 100644 index 0000000..da9b1ab --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wplacement-new-size-4.C @@ -0,0 +1,14 @@ +// PR c++/77804 - Internal compiler error on incorrect initialization of +// new-d array +// { dg-do compile } +// { dg-additional-options "-Wplacement-new -Wvla -Wno-error=vla" } + +void* operator new[] (__SIZE_TYPE__ n, void *p) { return p; } + +int main() +{ + char buf[256]; + unsigned n = 10; + int* p = new (buf) (int[n]); // { dg-warning "non-constant array new length must be specified without parentheses around the type-id" } + // { dg-warning "ISO C\\+\\+ forbids variable length array" "vla warning" { target *-*-* } .-1 } +}