From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 1140D3858C78 for ; Fri, 12 May 2023 12:32:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1140D3858C78 Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=westcontrol.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1pxRwk-0006pw-2l for gcc@gcc.gnu.org; Fri, 12 May 2023 14:32:30 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gcc@gcc.gnu.org From: David Brown Subject: Re: [wish] Flexible array members in unions Date: Fri, 12 May 2023 14:32:22 +0200 Message-ID: References: <44940599-7b43-99f6-5b09-4f050d645c7b@gmail.com> <202305111158.C78642624@keescook> <74ee73d2-04e-ea8-9430-93929446e925@codesourcery.com> <202305111410.CFE0875F@keescook> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Content-Language: en-GB In-Reply-To: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 12/05/2023 08:16, Richard Biener via Gcc wrote: > On Thu, May 11, 2023 at 11:14 PM Kees Cook via Gcc wrote: >> >> On Thu, May 11, 2023 at 08:53:52PM +0000, Joseph Myers wrote: >>> On Thu, 11 May 2023, Kees Cook via Gcc wrote: >>> >>>> On Thu, May 11, 2023 at 06:29:10PM +0200, Alejandro Colomar wrote: >>>>> On 5/11/23 18:07, Alejandro Colomar wrote: >>>>> [...] >>>>>> Would you allow flexible array members in unions? Is there any >>>>>> strong reason to disallow them? >>>> >>>> Yes please!! And alone in a struct, too. >>>> >>>> AFAICT, there is no mechanical/architectural reason to disallow them >>>> (especially since they _can_ be constructed with some fancy tricks, >>>> and they behave as expected.) My understanding is that it's disallowed >>>> due to an overly strict reading of the very terse language that created >>>> flexible arrays in C99. >>> >>> Standard C has no such thing as a zero-size object or type, which would >>> lead to problems with a struct or union that only contains a flexible >>> array member there. >> >> Ah-ha, okay. That root cause makes sense now. > > Hmm. but then the workaround > > struct X { > int n; > union u { > char at_least_size_one; > int iarr[]; > short sarr[]; > }; > }; > > doesn't work either. We could make that a GNU extension without > adverse effects? > > Richard. > I would like and use an extension like that (for C and C++) - the flexible arrays would act as though they were the same size as the size-specific part of the union, rounding up in this case to make the alignments correct. I regularly want something like : union ProtocolBuffer { struct { header ... data fields ... } uint8_t raw8[]; uint32_t raw32[]; } The "raw" arrays would be used to move data around, or access it from communication drivers. As C (and C++) is defined, I have to split this up so that the "raw" arrays can use "sizeof(ProtocolTelegram) / 4" or similar expressions for their size. If flexible arrays in unions were allowed here, it could make my code a little neater and use more anonymous unions and structs to reduce unhelpful verbosity. >> Why are zero-sized objects missing in Standard C? Or, perhaps, the better >> question is: what's needed to support the idea of a zero-sized object? >> >> -- >> Kees Cook >