A number of Fedora 24 packages have been observed to fail with the trunk of GCC 6 due to the new -Wplacement-new warning. At least some of those are due to the invalid but not entirely uncommon "idiom" of using placement to construct a larger object in the last member of a structure, where the member's type is an array of 1 element. An example of one such error was discussed in the following thread: https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/ELWZFEXE72PJWXUU7N5WKYXD4DXEPUFY/ The code representative of this "idiom" was reduced to the following small test case: #include <stdlib.h> #include <new> struct X { enum Type { Int, Double }; Type type; char data[1]; }; int main() { X* p = (X*)malloc(sizeof(X) + sizeof(double) -1); double* d = new (p->data) double(1.0); p->type = X::Double; } Since this code is questionable but not currently (with GCC 6) unsafe we want to avoid diagnosing it by default (-Wplacement-new is enabled by default). To make it possible to request a warning for such code, -Wplacement-new will be changed analogously to (for example) -Wshift-overflow to optionally take an integer argument: 1 or 2. With 1 (or without an argument), the diagnostic will not be issued for the code above. With 2, the code will be diagnosed the same way as it is now.
Working on it.
Patch posted for review: https://gcc.gnu.org/ml/gcc-patches/2016-02/msg00355.html
Author: msebor Date: Fri Feb 5 22:27:37 2016 New Revision: 233190 URL: https://gcc.gnu.org/viewcvs?rev=233190&root=gcc&view=rev Log: PR c++/69662 - -Wplacement-new on allocated one element array members gcc/testsuite/ChangeLog: PR c++/69662 * g++.dg/warn/Wplacement-new-size-1.C: New test. * g++.dg/warn/Wplacement-new-size-2.C: New test. gcc/cp/ChangeLog: PR c++/69662 * init.c (find_field_init): New function. (warn_placement_new_too_small): Call it. Handle one-element arrays at ends of structures special. gcc/c-family/ChangeLog: PR c++/69662 * c.opt (Warning options): Update -Wplacement-new to take an optional argument. gcc/ChangeLog: PR c++/69662 * doc/invoke.texi: Update -Wplacement-new to take an optional argument. Added: trunk/gcc/testsuite/g++.dg/warn/Wplacement-new-size-1.C trunk/gcc/testsuite/g++.dg/warn/Wplacement-new-size-2.C Modified: trunk/gcc/ChangeLog trunk/gcc/c-family/ChangeLog trunk/gcc/c-family/c.opt trunk/gcc/cp/ChangeLog trunk/gcc/cp/init.c trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog
Implemented in r233190.