[Bug c/53548] allow flexible array members in unions like zero-length arrays
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
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #13 from GCC Commits --- The master branch has been updated by Qing Zhao : https://gcc.gnu.org/g:93f6a47583f3fa8a1b66856ecb19ec28f26b2ba4 commit r15-210-g93f6a47583f3fa8a1b66856ecb19ec28f26b2ba4 Author: Qing Zhao Date: Mon May 6 16:27:09 2024 + 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #14 from GCC Commits --- The master branch has been updated by Qing Zhao : https://gcc.gnu.org/g:6634a409124a884ff66b3756568a7daae7d3c295 commit r15-211-g6634a409124a884ff66b3756568a7daae7d3c295 Author: Qing Zhao Date: Mon May 6 16:28:01 2024 + 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #11 from GCC Commits --- The master branch has been updated by Qing Zhao : https://gcc.gnu.org/g:adb1c8a0f167c3a1f7593d75f5a10eb07a5d741a commit r15-208-gadb1c8a0f167c3a1f7593d75f5a10eb07a5d741a Author: Qing Zhao Date: Mon May 6 16:25:04 2024 + 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #12 from GCC Commits --- The master branch has been updated by Qing Zhao : https://gcc.gnu.org/g:f27fc59d9f7c735d200fda647a487850144b10eb commit r15-209-gf27fc59d9f7c735d200fda647a487850144b10eb Author: Qing Zhao Date: Mon May 6 16:26:19 2024 + 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
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
[Bug c/53548] allow flexible array members in unions like zero-length arrays
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
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #8 from Kees Cook --- Clang bug: https://github.com/llvm/llvm-project/issues/84565
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #7 from Kees Cook --- 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 Kees Cook 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 --- 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).
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 Martin Sebor changed: What|Removed |Added Status|WAITING |RESOLVED Resolution|--- |WONTFIX --- Comment #5 from Martin Sebor --- Thanks! Resolving as WONTFIX with the submitter's consent in comment #3.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 --- Comment #4 from Mike Frysinger --- (In reply to Martin Sebor from comment #3) that's fine. thanks !
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 Martin Sebor changed: What|Removed |Added Status|UNCONFIRMED |WAITING Last reconfirmed||2015-12-04 Ever confirmed|0 |1 --- Comment #3 from Martin Sebor --- 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.
[Bug c/53548] allow flexible array members in unions like zero-length arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53548 Martin Sebor changed: What|Removed |Added CC||msebor at gcc dot gnu.org --- Comment #2 from Martin Sebor --- 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 }; ^
[Bug c/53548] allow flexible array members in unions like zero-length arrays
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.