https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69662

            Bug ID: 69662
           Summary: -Wplacement-new on allocated one element array members
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

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/de...@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.

Reply via email to