[clang] [Clang][Sema]: Allow flexible arrays in unions and alone in structs (PR #84428)

Kees Cook via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 11 16:15:33 PDT 2024


kees wrote:

> C99 added flexible array members, and the C99 rationale says the feature was added specifically as a replacement for the common idiom known as the "struct hack" for creating a structure containing a variable-size array.

This is my reasoning as well -- we (Linux dev hat on) have been converting a giant codebase from the "struct hack" to C99 flexible arrays, but the Standard (IMO) made a mistake in not recognizing the need for flexible arrays to be in unions. This was (and is) a well-used code pattern for the "struct hack" that has persisted for decades. Now, I'm not here to argue for a change to the Standard (that's a much different effort), but rather to argue that the existing "struck hack" _extensions_ be made to recognize the C99 flexible array as well, since otherwise it is not possible to cleanly update existing code.

The fact that Clang _already_ supports C99 flex arrays in unions (through other GNU extension) should serve as evidence of its sensible direct extension to C99 flexible arrays. The "not in unions" check can already be evaded by using the "empty struct" GNU extension. So, to be clear: I'm talking about making the _existing_ extensions be uniformly applied. I don't want to be distracted by whether or not this should be part of the C Standard -- I would like Clang to do what it already does but without requiring the pointless syntactic obfuscation currently in use by Linux.

https://github.com/llvm/llvm-project/pull/84428


More information about the cfe-commits mailing list