Bug 53548 - allow flexible array members in unions like zero-length arrays
Summary: allow flexible array members in unions like zero-length arrays
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 15.0
: P3 enhancement
Target Milestone: ---
Assignee: qinzhao
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-01 02:13 UTC by Mike Frysinger
Modified: 2024-07-31 01:16 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-12-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Frysinger 2012-06-01 02:13:22 UTC
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
Comment 1 Carlos O'Donell 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.
Comment 2 Martin Sebor 2015-11-22 00:09:45 UTC
The second example is rejected (with a slightly different error) because C specifies that:

   As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.

I.e., flexible array members may be defined only in structures, not in unions.

Accepting such code would be an extension to the language (which might be what you are proposing).  But since an equivalent extension already exists (zero-size array), I'm not sure what the value of adding another would be.

I tried to see if there was a way to use a flexible array member to achieve the effect you're looking for.  Here's what I came up with (it causes -Wpedantic warnings, but so does the first accepted example):

$ cat z.cpp && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -Wall -Wextra -Wpedantic -S -o/dev/null -xc z.cpp
struct {
    short op;
    union {
        int i;
        char foo[0];
    };
};

struct {
    short op;
    union {
        int i;
        struct {
            struct { } s;
            char foo[];
        };
    };
};
z.cpp:5:14: warning: ISO C forbids zero-size array ‘foo’ [-Wpedantic]
         char foo[0];
              ^~~

z.cpp:7:1: warning: unnamed struct/union that defines no instances
 };
 ^

z.cpp:14:13: warning: struct has no members [-Wpedantic]
             struct { } s;
             ^~~~~~

z.cpp:17:5: warning: invalid use of structure with flexible array member [-Wpedantic]
     };
     ^

z.cpp:18:1: warning: unnamed struct/union that defines no instances
 };
 ^
Comment 3 Martin Sebor 2015-12-04 03:54:41 UTC
Since the C standard requires the test case to be diagnosed and there is an existing extension that makes the requested functionality possible I'd like to close this as WONTFIX.  Mike, please let me know if you disagree.
Comment 4 Mike Frysinger 2015-12-04 05:05:07 UTC
(In reply to Martin Sebor from comment #3)

that's fine.  thanks !
Comment 5 Martin Sebor 2015-12-04 15:36:47 UTC
Thanks! Resolving as WONTFIX with the submitter's consent in comment #3.
Comment 6 Kees Cook 2024-03-08 20:41:50 UTC
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).
Comment 7 Kees Cook 2024-03-08 20:42:26 UTC
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.
Comment 8 Kees Cook 2024-03-08 20:50:30 UTC
Clang bug: https://github.com/llvm/llvm-project/issues/84565
Comment 9 qinzhao 2024-03-13 19:34:35 UTC
I think that we need to add this support as an GCC extension
Comment 10 qinzhao 2024-04-08 18:20:11 UTC
Clang has accept this extension:
https://github.com/llvm/llvm-project/pull/84428
Comment 11 GCC Commits 2024-05-06 18:36:25 UTC
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.
Comment 12 GCC Commits 2024-05-06 18:36:30 UTC
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.
Comment 13 GCC Commits 2024-05-06 18:36:35 UTC
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.
Comment 14 GCC Commits 2024-05-06 18:36:41 UTC
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.
Comment 15 qinzhao 2024-05-06 18:40:09 UTC
Fixed in GCC15
Comment 16 GCC Commits 2024-07-30 10:59:35 UTC
The master branch has been updated by Sam James <sjames@gcc.gnu.org>:

https://gcc.gnu.org/g:136f364e26d9ad4f05e0005e480813cdc8f56c96

commit r15-2403-g136f364e26d9ad4f05e0005e480813cdc8f56c96
Author: Sam James <sam@gentoo.org>
Date:   Tue Jul 30 11:08:31 2024 +0100

    testsuite: fix dg-do run whitespace
    
    This caused the tests to not be run. I may do further passes for non-run
    next.
    
    Tested on x86_64-pc-linux-gnu and checked test logs before/after.
    
            PR c/53548
            PR target/101529
            PR tree-optimization/102359
            * c-c++-common/fam-in-union-alone-in-struct-1.c: Fix whitespace in dg directive.
            * c-c++-common/fam-in-union-alone-in-struct-2.c: Likewise.
            * c-c++-common/torture/builtin-shufflevector-2.c: Likewise.
            * g++.dg/pr102359_2.C: Likewise.
            * g++.target/i386/mvc1.C: Likewise.