[2/2] Documentation Update.

Message ID 20230131141140.3610133-3-qing.zhao@oracle.com
State New
Headers
Series PR101832: Handle component_ref to a structure/union field including flexible array member for builtin_object_size |

Commit Message

Qing Zhao Jan. 31, 2023, 2:11 p.m. UTC
  Update documentation to clarify a GCC extension on structure with
flexible array member being nested in another structure.

gcc/ChangeLog:

	* doc/extend.texi: Document GCC extension on a structure containing
	a flexible array member to be a member of another structure.
---
 gcc/doc/extend.texi | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
  

Comments

Siddhesh Poyarekar Feb. 1, 2023, 4:55 p.m. UTC | #1
On 2023-01-31 09:11, Qing Zhao wrote:
> Update documentation to clarify a GCC extension on structure with
> flexible array member being nested in another structure.
> 
> gcc/ChangeLog:
> 
> 	* doc/extend.texi: Document GCC extension on a structure containing
> 	a flexible array member to be a member of another structure.

Should this resolve pr#77650 since the proposed action there appears to 
be to document these semantics?

Thanks,
Sid

> ---
>   gcc/doc/extend.texi | 35 ++++++++++++++++++++++++++++++++++-
>   1 file changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 4a89a3eae7c..54e4baf49a9 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -1748,7 +1748,40 @@ Flexible array members may only appear as the last member of a
>   A structure containing a flexible array member, or a union containing
>   such a structure (possibly recursively), may not be a member of a
>   structure or an element of an array.  (However, these uses are
> -permitted by GCC as extensions.)
> +permitted by GCC as extensions, see details below.)
> +@end itemize
> +
> +GCC extension accepts a structure containing a flexible array member, or
> +a union containing such a structure (possibly recursively) to be a member
> +of a structure.
> +
> +There are two situations:
> +
> +@itemize @bullet
> +@item
> +The structure with a flexible array member is the last field of another
> +structure, for example:
> +
> +@smallexample
> +struct flex  @{ int length; char data[]; @};
> +
> +struct out_flex @{ int m; struct flex flex_data; @};
> +@end smallexample
> +
> +In the above, @code{flex_data.data[]} is considered as a flexible array too.
> +
> +@item
> +The structure with a flexible array member is the middle field of another
> +structure, for example:
> +
> +@smallexample
> +struct flex  @{ int length; char data[]; @};
> +
> +struct mid_flex @{ int m; struct flex flex_data; int n; @};
> +@end smallexample
> +
> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly to
> +the padding. E.g, up to 4 elements.
>   @end itemize
>   
>   Non-empty initialization of zero-length
  
Qing Zhao Feb. 1, 2023, 6:24 p.m. UTC | #2
> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
> 
> On 2023-01-31 09:11, Qing Zhao wrote:
>> Update documentation to clarify a GCC extension on structure with
>> flexible array member being nested in another structure.
>> gcc/ChangeLog:
>> 	* doc/extend.texi: Document GCC extension on a structure containing
>> 	a flexible array member to be a member of another structure.
> 
> Should this resolve pr#77650 since the proposed action there appears to be to document these semantics?

My understanding of pr77650 is specifically for documentation on the following case:

The structure with a flexible array member is the middle field of another structure.

Which I added in the documentation as the 2nd situation.  
However, I am still not very comfortable on my current clarification on this situation: how should we document on 
the expected gcc behavior to handle such situation?

Qing
> 
> Thanks,
> Sid
> 
>> ---
>>  gcc/doc/extend.texi | 35 ++++++++++++++++++++++++++++++++++-
>>  1 file changed, 34 insertions(+), 1 deletion(-)
>> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
>> index 4a89a3eae7c..54e4baf49a9 100644
>> --- a/gcc/doc/extend.texi
>> +++ b/gcc/doc/extend.texi
>> @@ -1748,7 +1748,40 @@ Flexible array members may only appear as the last member of a
>>  A structure containing a flexible array member, or a union containing
>>  such a structure (possibly recursively), may not be a member of a
>>  structure or an element of an array.  (However, these uses are
>> -permitted by GCC as extensions.)
>> +permitted by GCC as extensions, see details below.)
>> +@end itemize
>> +
>> +GCC extension accepts a structure containing a flexible array member, or
>> +a union containing such a structure (possibly recursively) to be a member
>> +of a structure.
>> +
>> +There are two situations:
>> +
>> +@itemize @bullet
>> +@item
>> +The structure with a flexible array member is the last field of another
>> +structure, for example:
>> +
>> +@smallexample
>> +struct flex  @{ int length; char data[]; @};
>> +
>> +struct out_flex @{ int m; struct flex flex_data; @};
>> +@end smallexample
>> +
>> +In the above, @code{flex_data.data[]} is considered as a flexible array too.
>> +
>> +@item
>> +The structure with a flexible array member is the middle field of another
>> +structure, for example:
>> +
>> +@smallexample
>> +struct flex  @{ int length; char data[]; @};
>> +
>> +struct mid_flex @{ int m; struct flex flex_data; int n; @};
>> +@end smallexample
>> +
>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly to
>> +the padding. E.g, up to 4 elements.
>>  @end itemize
>>    Non-empty initialization of zero-length
  
Siddhesh Poyarekar Feb. 1, 2023, 6:57 p.m. UTC | #3
On 2023-02-01 13:24, Qing Zhao wrote:
> 
> 
>> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>>
>> On 2023-01-31 09:11, Qing Zhao wrote:
>>> Update documentation to clarify a GCC extension on structure with
>>> flexible array member being nested in another structure.
>>> gcc/ChangeLog:
>>> 	* doc/extend.texi: Document GCC extension on a structure containing
>>> 	a flexible array member to be a member of another structure.
>>
>> Should this resolve pr#77650 since the proposed action there appears to be to document these semantics?
> 
> My understanding of pr77650 is specifically for documentation on the following case:
> 
> The structure with a flexible array member is the middle field of another structure.
> 
> Which I added in the documentation as the 2nd situation.
> However, I am still not very comfortable on my current clarification on this situation: how should we document on
> the expected gcc behavior to handle such situation?

I reckon wording that dissuades programmers from using this might be 
appropriate, i.e. don't rely on this and if you already have such nested 
flex arrays, change code to remove them.

>>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly to
>>> +the padding. E.g, up to 4 elements.

"""
... Relying on space in struct padding is bad programming practice and 
any code relying on this behaviour should be modified to ensure that 
flexible array members only end up at the ends of arrays.  The 
`-pedantic` flag should help identify such uses.
"""

Although -pedantic will also flag on flex arrays nested in structs even 
if they're at the end of the parent struct, so my suggestion on the 
warning is not really perfect.

Sid
  
Qing Zhao Feb. 1, 2023, 7:19 p.m. UTC | #4
> On Feb 1, 2023, at 1:57 PM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
> 
> On 2023-02-01 13:24, Qing Zhao wrote:
>>> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>>> 
>>> On 2023-01-31 09:11, Qing Zhao wrote:
>>>> Update documentation to clarify a GCC extension on structure with
>>>> flexible array member being nested in another structure.
>>>> gcc/ChangeLog:
>>>> 	* doc/extend.texi: Document GCC extension on a structure containing
>>>> 	a flexible array member to be a member of another structure.
>>> 
>>> Should this resolve pr#77650 since the proposed action there appears to be to document these semantics?
>> My understanding of pr77650 is specifically for documentation on the following case:
>> The structure with a flexible array member is the middle field of another structure.
>> Which I added in the documentation as the 2nd situation.
>> However, I am still not very comfortable on my current clarification on this situation: how should we document on
>> the expected gcc behavior to handle such situation?
> 
> I reckon wording that dissuades programmers from using this might be appropriate, i.e. don't rely on this and if you already have such nested flex arrays, change code to remove them.

Good suggestion. 

> 
>>>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly to
>>>> +the padding. E.g, up to 4 elements.
> 
> """
> ... Relying on space in struct padding is bad programming practice and any code relying on this behaviour should be modified to ensure that flexible array members only end up at the ends of arrays.  The `-pedantic` flag should help identify such uses.
> """
> 
> Although -pedantic will also flag on flex arrays nested in structs even if they're at the end of the parent struct, so my suggestion on the warning is not really perfect.

Yes, both the situations (flex arrays nested in the end of of structures, or flex array nested in the middle of structures) are NOT standard-conforming, we might need to dis-encourage both situations?

Qing
> 
> Sid
  
Richard Biener Feb. 2, 2023, 8:33 a.m. UTC | #5
On Wed, 1 Feb 2023, Siddhesh Poyarekar wrote:

> On 2023-02-01 13:24, Qing Zhao wrote:
> > 
> > 
> >> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org>
> >> wrote:
> >>
> >> On 2023-01-31 09:11, Qing Zhao wrote:
> >>> Update documentation to clarify a GCC extension on structure with
> >>> flexible array member being nested in another structure.
> >>> gcc/ChangeLog:
> >>>  * doc/extend.texi: Document GCC extension on a structure containing
> >>>  a flexible array member to be a member of another structure.
> >>
> >> Should this resolve pr#77650 since the proposed action there appears to be
> >> to document these semantics?
> > 
> > My understanding of pr77650 is specifically for documentation on the
> > following case:
> > 
> > The structure with a flexible array member is the middle field of another
> > structure.
> > 
> > Which I added in the documentation as the 2nd situation.
> > However, I am still not very comfortable on my current clarification on this
> > situation: how should we document on
> > the expected gcc behavior to handle such situation?
> 
> I reckon wording that dissuades programmers from using this might be
> appropriate, i.e. don't rely on this and if you already have such nested flex
> arrays, change code to remove them.
> 
> >>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly
> >>> to
> >>> +the padding. E.g, up to 4 elements.
> 
> """
> ... Relying on space in struct padding is bad programming practice and any
> code relying on this behaviour should be modified to ensure that flexible
> array members only end up at the ends of arrays.  The `-pedantic` flag should
> help identify such uses.
> """
> 
> Although -pedantic will also flag on flex arrays nested in structs even if
> they're at the end of the parent struct, so my suggestion on the warning is
> not really perfect.

Wow, so I checked and we indeed accept

struct X { int n; int data[]; };
struct Y { struct X x; int end; };

and -pedantic says

t.c:2:21: warning: invalid use of structure with flexible array member 
[-Wpedantic]
    2 | struct Y { struct X x; int end; };
      |           

and clang reports

t.c:2:21: warning: field 'x' with variable sized type 'struct X' not at 
the end of a struct or class is a GNU extension 
[-Wgnu-variable-sized-type-not-at-end]
struct Y { struct X x; int end; };
                    ^

looking at PR77650 what seems missing there is the semantics of this
extension as expected/required by the glibc use.  comment#5 seems
to suggest that for my example above its expected that
Y.x.data[0] aliases Y.end?!  There must be a better way to write
the glibc code and IMHO it would be best to deprecate this extension.
Definitely the middle-end wouldn't consider this aliasing for
my example - maybe it "works" when wrapped inside a union but
then for sure only when the union is visible in all accesses ...

typedef union
{
  struct __gconv_info __cd;
  struct
  {
    struct __gconv_info __cd;
    struct __gconv_step_data __data;
  } __combined;
} _G_iconv_t;

could be written as

typedef union
{
  struct __gconv_info __cd;
  char __dummy[sizeof(struct __gconv_info) + sizeof(struct 
__gconv_step_data)];
} _G_iconv_t;

in case the intent is to provide a complete type with space for
a single __gconv_step_data.

Richard.
  
Qing Zhao Feb. 2, 2023, 2:31 p.m. UTC | #6
> On Feb 2, 2023, at 3:33 AM, Richard Biener <rguenther@suse.de> wrote:
> 
> On Wed, 1 Feb 2023, Siddhesh Poyarekar wrote:
> 
>> On 2023-02-01 13:24, Qing Zhao wrote:
>>> 
>>> 
>>>> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org>
>>>> wrote:
>>>> 
>>>> On 2023-01-31 09:11, Qing Zhao wrote:
>>>>> Update documentation to clarify a GCC extension on structure with
>>>>> flexible array member being nested in another structure.
>>>>> gcc/ChangeLog:
>>>>> * doc/extend.texi: Document GCC extension on a structure containing
>>>>> a flexible array member to be a member of another structure.
>>>> 
>>>> Should this resolve pr#77650 since the proposed action there appears to be
>>>> to document these semantics?
>>> 
>>> My understanding of pr77650 is specifically for documentation on the
>>> following case:
>>> 
>>> The structure with a flexible array member is the middle field of another
>>> structure.
>>> 
>>> Which I added in the documentation as the 2nd situation.
>>> However, I am still not very comfortable on my current clarification on this
>>> situation: how should we document on
>>> the expected gcc behavior to handle such situation?
>> 
>> I reckon wording that dissuades programmers from using this might be
>> appropriate, i.e. don't rely on this and if you already have such nested flex
>> arrays, change code to remove them.
>> 
>>>>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly
>>>>> to
>>>>> +the padding. E.g, up to 4 elements.
>> 
>> """
>> ... Relying on space in struct padding is bad programming practice and any
>> code relying on this behaviour should be modified to ensure that flexible
>> array members only end up at the ends of arrays.  The `-pedantic` flag should
>> help identify such uses.
>> """
>> 
>> Although -pedantic will also flag on flex arrays nested in structs even if
>> they're at the end of the parent struct, so my suggestion on the warning is
>> not really perfect.
> 
> Wow, so I checked and we indeed accept
> 
> struct X { int n; int data[]; };
> struct Y { struct X x; int end; };
> 
> and -pedantic says
> 
> t.c:2:21: warning: invalid use of structure with flexible array member 
> [-Wpedantic]
>    2 | struct Y { struct X x; int end; };
>      |    

Currently, -pedantic report the same message for flex arrays nested in structs at the end of the parent struct AND in the middle of the parent struct. 
Shall we distinguish them and report different warning messages in order to discourage the latter case? 

And at the same time, in the documentation, clarify these two situations, and discourage the latter case at the same time as well?
>       
> 
> and clang reports
> 
> t.c:2:21: warning: field 'x' with variable sized type 'struct X' not at 
> the end of a struct or class is a GNU extension 
> [-Wgnu-variable-sized-type-not-at-end]
> struct Y { struct X x; int end; };
>  
>                  ^

Clang’s warning message is clearer. 
> 
> looking at PR77650 what seems missing there is the semantics of this
> extension as expected/required by the glibc use.  comment#5 seems
> to suggest that for my example above its expected that
> Y.x.data[0] aliases Y.end?!

Should we mentioned this alias relationship in the doc?

>  There must be a better way to write
> the glibc code and IMHO it would be best to deprecate this extension.

Agreed. This is really a bad practice, should be deprecated. 
We can give warning first in this release, and then deprecate this extension in a latter release. 

> Definitely the middle-end wouldn't consider this aliasing for
> my example - maybe it "works" when wrapped inside a union but
> then for sure only when the union is visible in all accesses ...
> 
> typedef union
> {
>  struct __gconv_info __cd;
>  struct
>  {
>    struct __gconv_info __cd;
>    struct __gconv_step_data __data;
>  } __combined;
> } _G_iconv_t;
> 
> could be written as
> 
> typedef union
> {
>  struct __gconv_info __cd;
>  char __dummy[sizeof(struct __gconv_info) + sizeof(struct 
> __gconv_step_data)];
> } _G_iconv_t;
> 
> in case the intent is to provide a complete type with space for
> a single __gconv_step_data.

Since the current middle end doesn’t handle such case consistently, what should we document this case? 
Or just mentioned this case is not handled consistently in the compiler and will be deprecated in the future, 
 user should not depend on it and should rewrite their code?

I don’t think it worth the effort to update GCC to consistently handle this case in general.

What’s your opinion?

Qing


> 
> Richard.
  
Kees Cook Feb. 2, 2023, 5:05 p.m. UTC | #7
On Thu, Feb 02, 2023 at 02:31:53PM +0000, Qing Zhao wrote:
> 
> > On Feb 2, 2023, at 3:33 AM, Richard Biener <rguenther@suse.de> wrote:
> > 
> > On Wed, 1 Feb 2023, Siddhesh Poyarekar wrote:
> > 
> >> On 2023-02-01 13:24, Qing Zhao wrote:
> >>> 
> >>> 
> >>>> On Feb 1, 2023, at 11:55 AM, Siddhesh Poyarekar <siddhesh@gotplt.org>
> >>>> wrote:
> >>>> 
> >>>> On 2023-01-31 09:11, Qing Zhao wrote:
> >>>>> Update documentation to clarify a GCC extension on structure with
> >>>>> flexible array member being nested in another structure.
> >>>>> gcc/ChangeLog:
> >>>>> * doc/extend.texi: Document GCC extension on a structure containing
> >>>>> a flexible array member to be a member of another structure.
> >>>> 
> >>>> Should this resolve pr#77650 since the proposed action there appears to be
> >>>> to document these semantics?
> >>> 
> >>> My understanding of pr77650 is specifically for documentation on the
> >>> following case:
> >>> 
> >>> The structure with a flexible array member is the middle field of another
> >>> structure.
> >>> 
> >>> Which I added in the documentation as the 2nd situation.
> >>> However, I am still not very comfortable on my current clarification on this
> >>> situation: how should we document on
> >>> the expected gcc behavior to handle such situation?
> >> 
> >> I reckon wording that dissuades programmers from using this might be
> >> appropriate, i.e. don't rely on this and if you already have such nested flex
> >> arrays, change code to remove them.
> >> 
> >>>>> +In the above, @code{flex_data.data[]} is allowed to be extended flexibly
> >>>>> to
> >>>>> +the padding. E.g, up to 4 elements.
> >> 
> >> """
> >> ... Relying on space in struct padding is bad programming practice and any
> >> code relying on this behaviour should be modified to ensure that flexible
> >> array members only end up at the ends of arrays.  The `-pedantic` flag should
> >> help identify such uses.
> >> """
> >> 
> >> Although -pedantic will also flag on flex arrays nested in structs even if
> >> they're at the end of the parent struct, so my suggestion on the warning is
> >> not really perfect.
> > 
> > Wow, so I checked and we indeed accept
> > 
> > struct X { int n; int data[]; };
> > struct Y { struct X x; int end; };
> > 
> > and -pedantic says
> > 
> > t.c:2:21: warning: invalid use of structure with flexible array member 
> > [-Wpedantic]
> >    2 | struct Y { struct X x; int end; };
> >      |    
> 
> Currently, -pedantic report the same message for flex arrays nested in structs at the end of the parent struct AND in the middle of the parent struct. 
> Shall we distinguish them and report different warning messages in order to discourage the latter case? 
> 
> And at the same time, in the documentation, clarify these two situations, and discourage the latter case at the same time as well?
> >       
> > 
> > and clang reports
> > 
> > t.c:2:21: warning: field 'x' with variable sized type 'struct X' not at 
> > the end of a struct or class is a GNU extension 
> > [-Wgnu-variable-sized-type-not-at-end]
> > struct Y { struct X x; int end; };
> >  
> >                  ^
> 
> Clang’s warning message is clearer. 
> > 
> > looking at PR77650 what seems missing there is the semantics of this
> > extension as expected/required by the glibc use.  comment#5 seems
> > to suggest that for my example above its expected that
> > Y.x.data[0] aliases Y.end?!
> 
> Should we mentioned this alias relationship in the doc?
> 
> >  There must be a better way to write
> > the glibc code and IMHO it would be best to deprecate this extension.
> 
> Agreed. This is really a bad practice, should be deprecated. 
> We can give warning first in this release, and then deprecate this extension in a latter release. 

Right -- this can lead (at least) to type confusion and other problems
too. We've been trying to remove all of these overlaps in the Linux
kernel. I mention it the "Overlapping composite structure members"
section at https://people.kernel.org/kees/bounded-flexible-arrays-in-c
  
Siddhesh Poyarekar Feb. 3, 2023, 4:25 a.m. UTC | #8
On 2023-02-02 03:33, Richard Biener wrote:
> looking at PR77650 what seems missing there is the semantics of this
> extension as expected/required by the glibc use.  comment#5 seems
> to suggest that for my example above its expected that
> Y.x.data[0] aliases Y.end?!  There must be a better way to write
> the glibc code and IMHO it would be best to deprecate this extension.
> Definitely the middle-end wouldn't consider this aliasing for
> my example - maybe it "works" when wrapped inside a union but
> then for sure only when the union is visible in all accesses ...
> 
> typedef union
> {
>    struct __gconv_info __cd;
>    struct
>    {
>      struct __gconv_info __cd;
>      struct __gconv_step_data __data;
>    } __combined;
> } _G_iconv_t;
> 
> could be written as
> 
> typedef union
> {
>    struct __gconv_info __cd;
>    char __dummy[sizeof(struct __gconv_info) + sizeof(struct
> __gconv_step_data)];
> } _G_iconv_t;
> 
> in case the intent is to provide a complete type with space for
> a single __gconv_step_data.

I dug into this on the glibc end and it looks like this commit:

commit 63fb8f9aa9d19f85599afe4b849b567aefd70a36
Author: Zack Weinberg <zackw@panix.com>
Date:   Mon Feb 5 14:13:41 2018 -0500

     Post-cleanup 2: minimize _G_config.h.

ripped all of that gunk out.  AFAICT there's no use of struct 
__gconv_info anywhere else in the code.

I reckon it is safe to say now that glibc no longer needs this misfeature.

Sid
  
Qing Zhao Feb. 3, 2023, 2:52 p.m. UTC | #9
> On Feb 2, 2023, at 11:25 PM, Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
> 
> On 2023-02-02 03:33, Richard Biener wrote:
>> looking at PR77650 what seems missing there is the semantics of this
>> extension as expected/required by the glibc use.  comment#5 seems
>> to suggest that for my example above its expected that
>> Y.x.data[0] aliases Y.end?!  There must be a better way to write
>> the glibc code and IMHO it would be best to deprecate this extension.
>> Definitely the middle-end wouldn't consider this aliasing for
>> my example - maybe it "works" when wrapped inside a union but
>> then for sure only when the union is visible in all accesses ...
>> typedef union
>> {
>>   struct __gconv_info __cd;
>>   struct
>>   {
>>     struct __gconv_info __cd;
>>     struct __gconv_step_data __data;
>>   } __combined;
>> } _G_iconv_t;
>> could be written as
>> typedef union
>> {
>>   struct __gconv_info __cd;
>>   char __dummy[sizeof(struct __gconv_info) + sizeof(struct
>> __gconv_step_data)];
>> } _G_iconv_t;
>> in case the intent is to provide a complete type with space for
>> a single __gconv_step_data.
> 
> I dug into this on the glibc end and it looks like this commit:
> 
> commit 63fb8f9aa9d19f85599afe4b849b567aefd70a36
> Author: Zack Weinberg <zackw@panix.com>
> Date:   Mon Feb 5 14:13:41 2018 -0500
> 
>    Post-cleanup 2: minimize _G_config.h.
> 
> ripped all of that gunk out.  AFAICT there's no use of struct __gconv_info anywhere else in the code.
> 
> I reckon it is safe to say now that glibc no longer needs this misfeature.

Thanks a lot for the info.

Looks like it’s good time to start deprecating this misfeature from GCC.

Qing

> 
> Sid
  
Jeff Law Feb. 3, 2023, 3:56 p.m. UTC | #10
On 2/2/23 10:05, Kees Cook via Gcc-patches wrote:

> 
> Right -- this can lead (at least) to type confusion and other problems
> too. We've been trying to remove all of these overlaps in the Linux
> kernel. I mention it the "Overlapping composite structure members"
> section at https://people.kernel.org/kees/bounded-flexible-arrays-in-c
Good.  We found several of these when Martin S was doing his work on the 
diagnostics a few years back.  It wasn't hard to see what the intent 
was, but it struck me as a poor (ab)use of the feature and that I 
wouldn't be surprised if compilers might differ in their semantics 
around this stuff.

jeff
  
Joseph Myers Feb. 3, 2023, 8:55 p.m. UTC | #11
On Thu, 2 Feb 2023, Siddhesh Poyarekar wrote:

> I dug into this on the glibc end and it looks like this commit:
> 
> commit 63fb8f9aa9d19f85599afe4b849b567aefd70a36
> Author: Zack Weinberg <zackw@panix.com>
> Date:   Mon Feb 5 14:13:41 2018 -0500
> 
>     Post-cleanup 2: minimize _G_config.h.
> 
> ripped all of that gunk out.  AFAICT there's no use of struct __gconv_info
> anywhere else in the code.
> 
> I reckon it is safe to say now that glibc no longer needs this misfeature.

It would be worth testing whether any change warns anywhere else in glibc 
(not necessarily in installed headers).  And to have fixincludes for the 
installed _G_config.h from old glibc if we start rejecting such code.
  
Qing Zhao Feb. 3, 2023, 10:38 p.m. UTC | #12
Okay, thanks all for the comments and suggestions.

Based on the discussion so far, I have the following plan for resolving this issue:

In GCC13:

1. Add documentation in extend.texi to include all the following 3 cases as GCC extension:

Case 1: The structure with a flexible array member is the last field of another
structure, for example:

struct flex  { int length; char data[]; }
struct out_flex { int m; struct flex flex_data; }

In the above, flex_data.data[] is considered as a flexible array too.

Case 2: The structure with a flexible array member is the field of another union, for example:

struct flex1  { int length1; char data1[]; }
struct flex2  { int length2; char data2[]; }
union out_flex { struct flex1 flex_data1; struct flex2 flex_data2; }

In the above, flex_data1.data1[] or flex_data2.data2[] is considered as flexible arrays too.

Case 3: The structure with a flexible array member is the middle field of another
structure, for example:

struct flex  { int length; char data[]; }
struct out_flex { int m; struct flex flex_data; int n; }

In the above, flex_data.data[] is allowed to be extended flexibly to
the padding. E.g, up to 4 elements.

However, relying on space in struct padding is a bad programming practice,  compilers do not 
handle such extension consistently, and any code relying on this behavior should be modified
to ensure that flexible array members only end up at the ends of structures.

Please use warning option -Wgnu-variable-sized-type-not-at-end (to be consistent with CLANG) 
to identify all such cases in the source code and modify them. This extension will be deprecated
from gcc in the next release.

2. Add a new warning option -Wgnu-varaible-sized-type-not-at-end to warn such usage.

In GCC14:

1. Include this new warning -Wgnu-varaible-sized-type-not-at-end to -Wall
2. Deprecate this extension from GCC. (Or delay this to next release?).


Let me know any comments and suggestions?

thanks.

Qing



> On Feb 3, 2023, at 3:55 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> 
> On Thu, 2 Feb 2023, Siddhesh Poyarekar wrote:
> 
>> I dug into this on the glibc end and it looks like this commit:
>> 
>> commit 63fb8f9aa9d19f85599afe4b849b567aefd70a36
>> Author: Zack Weinberg <zackw@panix.com>
>> Date:   Mon Feb 5 14:13:41 2018 -0500
>> 
>>    Post-cleanup 2: minimize _G_config.h.
>> 
>> ripped all of that gunk out.  AFAICT there's no use of struct __gconv_info
>> anywhere else in the code.
>> 
>> I reckon it is safe to say now that glibc no longer needs this misfeature.
> 
> It would be worth testing whether any change warns anywhere else in glibc 
> (not necessarily in installed headers).  And to have fixincludes for the 
> installed _G_config.h from old glibc if we start rejecting such code.
> 
> -- 
> Joseph S. Myers
> joseph@codesourcery.com
  

Patch

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 4a89a3eae7c..54e4baf49a9 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -1748,7 +1748,40 @@  Flexible array members may only appear as the last member of a
 A structure containing a flexible array member, or a union containing
 such a structure (possibly recursively), may not be a member of a
 structure or an element of an array.  (However, these uses are
-permitted by GCC as extensions.)
+permitted by GCC as extensions, see details below.)
+@end itemize
+
+GCC extension accepts a structure containing a flexible array member, or
+a union containing such a structure (possibly recursively) to be a member
+of a structure.
+
+There are two situations:
+
+@itemize @bullet
+@item
+The structure with a flexible array member is the last field of another
+structure, for example:
+
+@smallexample
+struct flex  @{ int length; char data[]; @};
+
+struct out_flex @{ int m; struct flex flex_data; @};
+@end smallexample
+
+In the above, @code{flex_data.data[]} is considered as a flexible array too.
+
+@item
+The structure with a flexible array member is the middle field of another
+structure, for example:
+
+@smallexample
+struct flex  @{ int length; char data[]; @};
+
+struct mid_flex @{ int m; struct flex flex_data; int n; @};
+@end smallexample
+
+In the above, @code{flex_data.data[]} is allowed to be extended flexibly to
+the padding. E.g, up to 4 elements.
 @end itemize
 
 Non-empty initialization of zero-length