Bug 69662 - -Wplacement-new on allocated one element array members
Summary: -Wplacement-new on allocated one element array members
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 enhancement
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2016-02-04 00:15 UTC by Martin Sebor
Modified: 2016-05-27 16:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-02-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-02-04 00:15:26 UTC
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.
Comment 1 Martin Sebor 2016-02-04 00:16:15 UTC
Working on it.
Comment 2 Martin Sebor 2016-02-04 20:23:58 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2016-02/msg00355.html
Comment 3 Martin Sebor 2016-02-05 22:28:09 UTC
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
Comment 4 Martin Sebor 2016-02-05 22:29:01 UTC
Implemented in r233190.