public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53548] New: allow flexible array members in unions like zero-length arrays
@ 2012-06-01  2:13 vapier at gentoo dot org
  2012-06-04  0:50 ` [Bug c/53548] " carlos_odonell at mentor dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: vapier at gentoo dot org @ 2012-06-01  2:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548

             Bug #: 53548
           Summary: allow flexible array members in unions like
                    zero-length arrays
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: vapier@gentoo.org


gcc will accept this code:
struct {
    short op;
    union {
        int i;
        char foo[0];
    };
};

but rejects this:
struct {
    short op;
    union {
        int i;
        char foo[];
    };
};

with the error:
error: flexible array member in otherwise empty struct


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
@ 2012-06-04  0:50 ` carlos_odonell at mentor dot com
  2024-03-08 20:41 ` kees at outflux dot net
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: carlos_odonell at mentor dot com @ 2012-06-04  0:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548

Carlos O'Donell <carlos_odonell at mentor dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |carlos_odonell at mentor
                   |                            |dot com

--- Comment #1 from Carlos O'Donell <carlos_odonell at mentor dot com> 2012-06-04 00:49:55 UTC ---
There are several practical cases where it would be nice to support this kind
of construct. Structures that *end* with a variable length arrays are used in
networking to represent a variable length result. In particular the example in
this issue is taken from a recent reworking of tftp.h in glibc which uses a
similar construct.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
  2012-06-04  0:50 ` [Bug c/53548] " carlos_odonell at mentor dot com
@ 2024-03-08 20:41 ` kees at outflux dot net
  2024-03-08 20:42 ` kees at outflux dot net
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: kees at outflux dot net @ 2024-03-08 20:41 UTC (permalink / raw)
  To: gcc-bugs

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

Kees Cook <kees at outflux dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |carlos at gcc dot gnu.org,
                   |                            |kees at outflux dot net,
                   |                            |ndesaulniers at google dot com,
                   |                            |qing.zhao at oracle dot com

--- Comment #6 from Kees Cook <kees at outflux dot net> ---
There is still no way to use C99 flexible arrays in unions (or alone in
structs) without syntactic obfuscation. The extension that already allows
0-sized arrays in unions should be extended to cover C99 arrays. This is
especially important for projects migrating away from the various deprecated
"fake" flexible array members to C99 flex array members, as they continue to
depend on both union membership and single-member structs (i.e. the Linux
kernel has lots of these, some even in UAPI).

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
  2012-06-04  0:50 ` [Bug c/53548] " carlos_odonell at mentor dot com
  2024-03-08 20:41 ` kees at outflux dot net
@ 2024-03-08 20:42 ` kees at outflux dot net
  2024-03-08 20:50 ` kees at outflux dot net
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: kees at outflux dot net @ 2024-03-08 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Kees Cook <kees at outflux dot net> ---
There is still no way to use C99 flexible arrays in unions (or alone in
structs) without syntactic obfuscation. The extension that already allows
0-sized arrays in unions should be extended to cover C99 arrays. This is
especially important for projects migrating away from the various deprecated
"fake" flexible array members to C99 flex array members, as they continue to
depend on both union membership and single-member structs (i.e. the Linux
kernel has lots of these, some even in UAPI).

Please reopen this bug. :) Clang is also preparing to fix this issue.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (2 preceding siblings ...)
  2024-03-08 20:42 ` kees at outflux dot net
@ 2024-03-08 20:50 ` kees at outflux dot net
  2024-03-13 19:34 ` qinzhao at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: kees at outflux dot net @ 2024-03-08 20:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Kees Cook <kees at outflux dot net> ---
Clang bug: https://github.com/llvm/llvm-project/issues/84565

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (3 preceding siblings ...)
  2024-03-08 20:50 ` kees at outflux dot net
@ 2024-03-13 19:34 ` qinzhao at gcc dot gnu.org
  2024-04-08 18:20 ` qinzhao at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: qinzhao at gcc dot gnu.org @ 2024-03-13 19:34 UTC (permalink / raw)
  To: gcc-bugs

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

qinzhao at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qinzhao at gcc dot gnu.org
             Status|RESOLVED                    |REOPENED
         Resolution|WONTFIX                     |---

--- Comment #9 from qinzhao at gcc dot gnu.org ---
I think that we need to add this support as an GCC extension

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (4 preceding siblings ...)
  2024-03-13 19:34 ` qinzhao at gcc dot gnu.org
@ 2024-04-08 18:20 ` qinzhao at gcc dot gnu.org
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: qinzhao at gcc dot gnu.org @ 2024-04-08 18:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from qinzhao at gcc dot gnu.org ---
Clang has accept this extension:
https://github.com/llvm/llvm-project/pull/84428

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (5 preceding siblings ...)
  2024-04-08 18:20 ` qinzhao at gcc dot gnu.org
@ 2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-06 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:adb1c8a0f167c3a1f7593d75f5a10eb07a5d741a

commit r15-208-gadb1c8a0f167c3a1f7593d75f5a10eb07a5d741a
Author: Qing Zhao <qing.zhao@oracle.com>
Date:   Mon May 6 16:25:04 2024 +0000

    Allow flexible array members in unions and alone in structures [PR53548]

    The request for GCC to accept that the C99 flexible array member can be
    in a union or alone in a structure has been made a long time ago around
2012
    for supporting several practical cases including glibc.

    A GCC PR has been opened for such request at that time:

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

    However, this PR was closed as WONTFIX around 2015 due to the following
reason:

    "there is an existing extension that makes the requested functionality
possible"
    i.e GCC fully supported that the zero-length array can be in a union or
alone
    in a structure for a long time. (though I didn't see any official
documentation
    on such extension)

    It's reasonable to close PR53548 at that time since zero-length array
extension
    can be used for such purpose.

    However, since GCC13, in order to improve the C/C++ security, we introduced
    -fstrict-flex-arrays=n to gradually eliminate the "fake flexible array"
    usages from C/C++ source code. As a result, zero-length arrays eventually
    will be replaced by C99 flexiable array member completely.

    Therefore, GCC needs to explicitly allow such extensions directly for C99
    flexible arrays, since flexable array member in unions or alone in structs
    are common code patterns in active use by the Linux kernel (and other
projects).

    For example, these do not error by default with GCC:

    union one {
      int a;
      int b[0];
    };

    union two {
      int a;
      struct {
        struct { } __empty;
        int b[];
      };
    };

    But these do:

    union three {
      int a;
      int b[];
    };

    struct four {
      int b[];
    }

    Clang has supported such extensions since March, 2024
    https://github.com/llvm/llvm-project/pull/84428

    GCC should also support such extensions. This will allow for
    a seamless transition for code bases away from zero-length arrays without
    losing existing code patterns.

    gcc/ChangeLog:

            PR c/53548
            * doc/extend.texi: Add documentation for Flexible Array Members in
            Unions and Flexible Array Members alone in Structures.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (6 preceding siblings ...)
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
@ 2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-06 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:f27fc59d9f7c735d200fda647a487850144b10eb

commit r15-209-gf27fc59d9f7c735d200fda647a487850144b10eb
Author: Qing Zhao <qing.zhao@oracle.com>
Date:   Mon May 6 16:26:19 2024 +0000

    C and C++ FE changes to support flexible array members in unions and alone
in structures. Adjust testcases for flexible array member in union and alone in
structure extension.

    PR c/53548

    gcc/c/ChangeLog:

            PR c/53548
            * c-decl.cc (finish_struct): Change errors to pedwarns for the
cases
            flexible array members in union or alone in structures.

    gcc/cp/ChangeLog:

            PR c/53548
            * class.cc (diagnose_flexarrays): Change error to pdewarn for the
case
            flexible array members alone in structures.
            * decl.cc (grokdeclarator): Change error to pdewarn for the case
            flexible array members in unions.

    gcc/ChangeLog:

            PR c/53548
            * stor-layout.cc (place_union_field): Use zero sizes for flexible
array
            member fields.

    gcc/testsuite/ChangeLog:

            PR c/53548
            * c-c++-common/builtin-clear-padding-3.c: Adjust testcase.
            * g++.dg/ext/flexary12.C: Likewise.
            * g++.dg/ext/flexary19.C: Likewise.
            * g++.dg/ext/flexary2.C: Likewise.
            * g++.dg/ext/flexary3.C: Likewise.
            * g++.dg/ext/flexary36.C: Likewise.
            * g++.dg/ext/flexary4.C: Likewise.
            * g++.dg/ext/flexary5.C: Likewise.
            * g++.dg/ext/flexary8.C: Likewise.
            * g++.dg/torture/pr64280.C: Likewise.
            * gcc.dg/20050620-1.c: Likewise.
            * gcc.dg/940510-1.c: Likewise.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (7 preceding siblings ...)
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
@ 2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
  2024-05-06 18:40 ` qinzhao at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-06 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:93f6a47583f3fa8a1b66856ecb19ec28f26b2ba4

commit r15-210-g93f6a47583f3fa8a1b66856ecb19ec28f26b2ba4
Author: Qing Zhao <qing.zhao@oracle.com>
Date:   Mon May 6 16:27:09 2024 +0000

    Add testing cases for flexible array members in unions and alone in
structures.

    PR c/53548

    gcc/testsuite/ChangeLog:

            PR c/53548
            * c-c++-common/fam-in-union-alone-in-struct-1.c: New testcase.
            * c-c++-common/fam-in-union-alone-in-struct-2.c: New testcase.
            * c-c++-common/fam-in-union-alone-in-struct-3.c: New testcase.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (8 preceding siblings ...)
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
@ 2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
  2024-05-06 18:40 ` qinzhao at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-06 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:6634a409124a884ff66b3756568a7daae7d3c295

commit r15-211-g6634a409124a884ff66b3756568a7daae7d3c295
Author: Qing Zhao <qing.zhao@oracle.com>
Date:   Mon May 6 16:28:01 2024 +0000

    Update the C FE routine "add_flexible_array_elts_to_size" C++ FE routine
"layout_var_decl" to handle the cases when the DECL is union.

    PR c/53548

    Add testing cases to test the _bos for flexible array members in unions
    or alone in structures.

    gcc/c/ChangeLog:

            PR c/53548
            * c-decl.cc (add_flexible_array_elts_to_size): Handle the cases
            when the DECL is union.

    gcc/cp/ChangeLog:

            PR c/53548
            * decl.cc (layout_var_decl): Handle the cases when the DECL is
            union with a flexible array member initializer.

    gcc/testsuite/ChangeLog:

            PR c/53548
            * c-c++-common/fam-in-union-alone-in-struct-bos-1.c: New test.
            * c-c++-common/fam-in-union-alone-in-struct-bos.c: New test.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/53548] allow flexible array members in unions like zero-length arrays
  2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
                   ` (9 preceding siblings ...)
  2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
@ 2024-05-06 18:40 ` qinzhao at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: qinzhao at gcc dot gnu.org @ 2024-05-06 18:40 UTC (permalink / raw)
  To: gcc-bugs

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

qinzhao at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unknown                     |15.0
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #15 from qinzhao at gcc dot gnu.org ---
Fixed in GCC15

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-06 18:40 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-01  2:13 [Bug c/53548] New: allow flexible array members in unions like zero-length arrays vapier at gentoo dot org
2012-06-04  0:50 ` [Bug c/53548] " carlos_odonell at mentor dot com
2024-03-08 20:41 ` kees at outflux dot net
2024-03-08 20:42 ` kees at outflux dot net
2024-03-08 20:50 ` kees at outflux dot net
2024-03-13 19:34 ` qinzhao at gcc dot gnu.org
2024-04-08 18:20 ` qinzhao at gcc dot gnu.org
2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
2024-05-06 18:36 ` cvs-commit at gcc dot gnu.org
2024-05-06 18:40 ` qinzhao at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).