Bug 81234 - [6 Regression] stray notes for a flexible array member not at end of struct
Summary: [6 Regression] stray notes for a flexible array member not at end of struct
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.3.1
: P3 minor
Target Milestone: 6.5
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: flexmembers
  Show dependency treegraph
 
Reported: 2017-06-27 17:02 UTC by Konstantin Kharlamov
Modified: 2018-10-26 11:52 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 7.1.0, 8.0
Known to fail: 6.3.1
Last reconfirmed: 2017-06-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Konstantin Kharlamov 2017-06-27 17:02:50 UTC
This code used to work prior to gcc-7.1 (it worked on 4.x, and — I just tested — 6.3 works too, with a warning though):

	struct DataPacket {
		/* … */
		unsigned szdata;
		char data[];
	};

	struct AckPacket {
		DataPacket header;
		int a,b,c;
	};

	int main() { return 0; }

However gcc-7.1 bails out with:

	λ g++ test.cpp -o a                    
	test.cpp:4:12: error: flexible array member ‘DataPacket::data’ not at end of ‘struct AckPacket’
	  char data[];
				^
	test.cpp:9:6: note: next member ‘int AckPacket::a’ declared here
	  int a,b,c; /* no data below */
		  ^
	test.cpp:7:8: note: in the definition of ‘struct AckPacket’
	 struct AckPacket {

Imo the code is fine. Though I'm open to suggestions into a better design too.
Comment 1 Jonathan Wakely 2017-06-27 17:33:05 UTC
The code is valid in C, so presumably should be valid with G++ too.

GCC 6 gives a warning with -pedantic, but prints the accompanying notes even without -pedantic!


$ g++ foo.c -pedantic
foo.c:4:27: warning: flexible array member ‘DataPacket::data’ not at end of ‘struct AckPacket’ [-Wpedantic]
                 char data[];
                           ^
foo.c:9:21: note: next member ‘int AckPacket::a’ declared here
                 int a,b,c;
                     ^
foo.c:7:16: note: in the definition of ‘struct AckPacket’
         struct AckPacket {
                ^~~~~~~~~


$ g++ foo.c 
foo.c:9:21: note: next member ‘int AckPacket::a’ declared here
                 int a,b,c;
                     ^
foo.c:7:16: note: in the definition of ‘struct AckPacket’
         struct AckPacket {
                ^~~~~~~~~
Comment 2 Jonathan Wakely 2017-06-27 17:36:00 UTC
The orhpaned notes started with r231665 and the pedwarn became an error with r241143
Comment 3 Martin Sebor 2017-06-27 19:24:27 UTC
The C constraint is that a flexible array member must be the last member of a struct.  In addition, such a struct may not not be used to define a member in another struct (not even as the last member).  These constraints prevent aliasing between such a member and those that follow.

In C, mode GCC accepts the test case from comment #0 but with -Wpedantic issues a warning:

t.c:8:14: warning: invalid use of structure with flexible array member [-Wpedantic]

G++ 7 is intentionally more strict here and rejects the code (the checking was tightened up in r241143).  The stray notes in GCC 6 (without -Wpedantic) are a 6.3 regression.

To avoid the error (and accept the risks of the array aliasing with members stored at the same address) define data as a zero-length array (https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html).
Comment 4 Jonathan Wakely 2017-06-28 07:21:23 UTC
(In reply to Martin Sebor from comment #3)
> In C, mode GCC accepts the test case from comment #0 but with -Wpedantic
> issues a warning:
> 
> t.c:8:14: warning: invalid use of structure with flexible array member
> [-Wpedantic]

Ah, I missed that when checking with the C front end, thanks.
Comment 5 Richard Biener 2017-07-04 08:44:56 UTC
GCC 6.4 is being released, adjusting target milestone.
Comment 6 Jakub Jelinek 2018-10-26 11:52:04 UTC
Not a bug.